0


I need a simple status/announcement system on my webpage.
Basically I want to make a small form with combobox (with three options) and a submit button. Depending on the choice of an option, I want it to show various messages (some php/jquery is required I guess). These messages should stay the same after refreshing (so I guess I need to store somewhere an information what was the last, selected option and do some if statements). I also need it to work for any user without refreshing (I found out that I should use AJAX here).
The following codes are the effects of my experiments and they may look horribly chaotic.
Also the following solution generally works except dynamic change for different user. He will see the new message after refreshing.
Thanks in advance.
Here is what I've got:

Index.php:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
        <div class="ann">
            <?php
                $con = mysql_connect("localhost","username","password") or die('Error while connecting to db server'); ;
                mysql_select_db("test",$con) or die('Error while selecting db'); ;
    // Check connection

                $zap = "SELECT mode FROM mode";
                $result = mysql_query($zap, $con);
                $current_id = mysql_fetch_row($result);
                $current_id = $current_id[0];

                if ($current_id == 1) {
                    echo "current id equals 1";
                }
                else if ($current_id == 2) {
                    echo "current id equals 2";
                } 
                else if ($current_id == 3) {
                    echo "current id equals 3";
                }

            ?>

        </div>

        <form method="post" id="asd">
            <select name="sel">
                <option value="1">one</option>
                <option value="2">two</option>
                <option value="3">three</option>
            </select>
            <input type="submit" value="OK" id="sub" />
        </form>

        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>     
        <script type="text/javascript" src="func.js"></script>

    </body>
</html>

process.php:

<?php
   require "MyConnect.php"; 
   connection();
   $option = isset($_POST['sel']) ? $_POST['sel'] : false;
   if($option) {
        echo htmlentities($_POST['sel'], ENT_QUOTES, "UTF-8");
   } else {
        echo "task option is required";
        exit; 
   }
   $zapp = "UPDATE mode SET mode = '".$option."'";
   $idzapp = mysql_query($zapp);
?>

MyConnect.php:

<?php     
function connection() { 
    $mysql_server = "localhost"; 
    $mysql_admin = "username"; 
    $mysql_pass = "password"; 
    $mysql_db = "test"; 
    @mysql_connect($mysql_server, $mysql_admin, $mysql_pass) 
    or die('Error while connecting to db server'); 
    @mysql_select_db($mysql_db) 
    or die('Error while connecting to db'); 
} 
?>

func.js:

$("#asd").submit(function() {

    var url = "process.php";

    $.ajax({
           type: "POST",
           url: url,
           data: $("#asd").serialize(),
           success: function(data)
           {
           $('.ann').html(data);
           }
         });

    return false;
});
Szosteczka
  • 143
  • 1
  • 7

0 Answers0