-2

This is how I build a lookup system, but it is such that when it sends it to the database. then you go to the site again to display the infomation you have been written to the page.

So must have one or other system that can fetch the latest infomation to the side, but if you click F5 to send it just infomation to the page. so must have done something that will be sent to a file.

Only if some content is not empty, and you have click on part. So just hear you on some help to do it or imitate.

I hope to only be something ajax as download infomation to the side. or jquery or similar which can just infomation by itself.

<?php
                if($_SESSION["logged_in"])
                {
                echo "<div class=\"opslag-indhold\">";

                ?>
                <form name="opslag" method="post" action="#">
                    <input type="text" name="opslag_indhold" id="del_indhold">
                    <input type="submit" name="opslag" value="Del" id="del">
                </form>
                <?php
                if(isset($_POST["opslag"]))
                {
                    if(!empty($_POST["opslag_indhold"]))
                    {
                        if($stmt = $mysqli->prepare('INSERT INTO `opslag` (`tekst`, `id_bruger`) VALUES (?, ?)'))
                        {
                            $stmt->bind_param('si', $tekst, $id_bruger);

                            $tekst = $_POST["opslag_indhold"];
                            $id_bruger = $_SESSION["id"];

                            $stmt->execute();
                            $stmt->close();

                            echo "Du har skrevet indhold";

                        }
                    }
                    else
                    {
                        echo "Du skal skrive noget";
                    }
                }
                ?>
                </div>
                <?php
                }
                ?>

If you have an idea please feel free to ask me

You are welcome to ask me if you want to know something?

EIDT MORE!

<?php
                if($_SESSION["logged_in"])
                {
                echo "<div class=\"opslag-indhold\">";

                ?>
                <form name="opslag" method="post" action="#">
                    <input type="text" name="opslag_indhold" id="del_indhold">
                    <input type="submit" name="opslag" value="Del" id="del">
                </form>
                <?php
                if(isset($_POST["opslag"]))
                {
                    if(!empty($_POST["opslag_indhold"]))
                    {
                        if($stmt = $mysqli->prepare('INSERT INTO `opslag` (`tekst`, `id_bruger`) VALUES (?, ?)'))
                        {
                            $stmt->bind_param('si', $tekst, $id_bruger);

                            $tekst = $_POST["opslag_indhold"];
                            $id_bruger = $_SESSION["id"];

                            $stmt->execute();
                            $stmt->close();

                            echo "Du har skrevet indhold";

                        }
                    }
                    else
                    {
                        echo "Du skal skrive noget";
                    }
                }
                ?>
                </div>
                <?php
                }
                ?>

here there will be posting to the page

<div class="opslag_midt-indhold">
            <?php
                if ($stmt = $mysqli->prepare('SELECT `tekst`, `id_bruger` FROM `opslag`ORDER BY `opslag`.`id_opslag` DESC LIMIT 15')) { 
                $stmt->execute();
                $stmt->store_result();
                $stmt->bind_result($tekst, $id_bruger);

                while ($stmt->fetch())
                {
                ?>
                <div class="opslag_bruger_info_profil">
                        <div class="pas-indhold">
                        <?php
                        if($id_bruger == $_SESSION["id"])
                        {
                            if ($stmt_1 = $mysqli->prepare('SELECT `brugernavn`, `profilbillede`, `profilbillede_godkendt` FROM `bruger` WHERE `id_bruger` = ?'))
                            { 
                                $stmt_1->bind_param('i', $id);
                                $id = $id_bruger;

                                /* Eksekver forespørgslen */
                                $stmt_1->execute();

                                /* Bind resultatet */
                                $stmt_1->bind_result($brugernavn, $profilbillede, $profilbillede_godkendt);

                                /* Hent rækker og udskriv data */
                                while ($stmt_1->fetch())
                                {
                                    if($profilbillede_godkendt == 0)
                                    {
                                        echo "<a href=\"/profil/$id_bruger/\"><img src=\"/profil/users.png\" alt=\"\" height=\"115\" width=\"100\" border=\"0\"></a>";
                                    }
                                    else
                                    {
                                        echo "<a href=\"/profil/$id_bruger/\"><img src=\"/profil/$profilbillede\" alt=\"\" height=\"115\" width=\"100\" border=\"0\"></a>";
                                    }
                                }
                                $stmt_1->close();
                            }
                        }
                        else
                        {
                            if ($stmt_2 = $mysqli->prepare('SELECT `brugernavn`, `profilbillede`, `profilbillede_godkendt` FROM `bruger` WHERE `id_bruger` = ?'))
                            { 
                                $stmt_2->bind_param('i', $id);
                                $id = $id_bruger;

                                /* Eksekver forespørgslen */
                                $stmt_2->execute();

                                /* Bind resultatet */
                                $stmt_2->bind_result($brugernavn, $profilbillede, $profilbillede_godkendt);

                                /* Hent rækker og udskriv data */
                                while ($stmt_2->fetch())
                                {
                                    if($profilbillede_godkendt == 0)
                                    {
                                        echo "<a href=\"/profil/$id_bruger/\"><img src=\"/profil/users.png\" alt=\"\" height=\"115\" width=\"100\" border=\"0\"></a>";
                                    }
                                    else
                                    {
                                        echo "<a href=\"/profil/$id_bruger/\"><img src=\"/profil/$profilbillede\" alt=\"\" height=\"115\" width=\"100\" border=\"0\"></a>";

                                    }
                                }
                                $stmt_2->close();
                            }
                        }
                        ?>
                        <?php echo $tekst;?>
                        </div>
                    </div>
                <?php
                }
                $stmt->close();
            } else {
                echo 'Der opstod en fejl i erklæringen: ' . $mysqli->error;
            }
            ?>
            </div>
  • It is not entirely clear what you're asking, but I'll take a guess that you need [Post-Redirect-Get](http://stackoverflow.com/questions/10827242/understanding-post-redirect-get) . – DCoder Sep 22 '12 at 11:45
  • I do not think you understand what my problem is. it must be such that it check whether you have click on submit and share content is not empty then its end me to the file which, after lying on the database and then send it to him / her back to the side. – Jesper Petersen Sep 22 '12 at 11:52
  • I don't understand that either. – ghoti Sep 22 '12 at 13:38

1 Answers1

0

What you need is ajax, best with jQuery.

You can use it like so:

$('#mybutton').on('click',function(){
    $.ajax({
        type: 'POST', //define post or get
        data: 'id=1&kind=bla', //replace with your needed parameters that you can retrieve as post paramaters in the called php script like $_POST['id'] and $_POST['kind']
        url: yourphpscript.php //call the php script that does something for you
        success: function(feedback){ //feedback will store everything that the php script echoes
            alert(feedback); //do something here with normal javascript/jQuery code
        }
    }).error(function(){ //Handle errors, e.g. unavailability of your php script
        alert('Something went wrong!');
    });
});

Checking whether content is not empty etc should be done in php, like any other validation. Never only validate client side!

In the body of the success function you can do stuff like appending divs, changing html inside an element, etc.

Your php script should like somewhat like the following:

<?php
    $id=$_POST['id'];
    $kind=$_POST['kind'];

    //Save some stuff in the db

    //Echo out whatever shall be displayed in the browser, e.g.
    echo "Du har skrevet indhold";
?>

Then the js var feedback will hold the value "Du har skrevet indhold".

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115