0

I have created a webpage using php and mysql, the code works fine except the problem is I have included so many submit buttons and hence my webpage gets redrawn many times. I lose some values i.e. $emirate value and $areas value. I used a variable $choice for storing those values but since the page is being redrawn, I don't get the values. Help me please.

$data = mysql_query("SELECT * FROM emirate") 
    or die(mysql_error()); 

    $choice="";

    Print "<table border cellpadding=3>"; 
    print"<tr><th><form method=get action=''><select id=EMIRATE size=1 name='EMIRATE' class='comboBoo'>";
    while($info = mysql_fetch_array( $data )) 
    { 
        Print "<option value=". $info['em_name'] .">".$info['em_name']."</option>"; 
    }
    print"</select><input type=submit value=OK></form></th>";
    if(isset($_GET['EMIRATE'])){
        $emirate=$_GET['EMIRATE'];
        $choice.=$emirate;
        $data = mysql_query("SELECT a_name FROM areas where em_id=(select em_id from emirate where em_name=\"".$emirate."\")") 
        or die(mysql_error());
        Print "<th><form method=get action=''><select id='AREAS' name=AREAS size=1 class='comboBoo'><OPTION value=ALL>ALL</option><OPTION value=ALL>ALL</option>";
        while($info = mysql_fetch_array( $data ))
        {
            Print "<option >".$info['a_name']."</option>";
        }
        print"</select><input type=submit value=OK></form></th>";
    }

    $choice.="->";

    if(isset($_GET['AREAS'])){
        $areas=$_GET['AREAS'];
        $choice.=$areas;
        $data = mysql_query("SELECT h_name FROM hypermarket_em where a_id=(select a_id from areas where a_name=\"".$areas."\")")
        or die(mysql_error());
        Print "<th><form method=get action=''><select name=HYPERMARKETS size=1 class='comboBoo'>";
        while($info = mysql_fetch_array( $data ))
        {
            Print "<option>".$info['h_name']."</option>";
        }
        print"</select><input type=submit value=OK></form></th>";
    }   
    Print "</tr></table>";
    Print "<table border cellpadding=3><tr><th>".$choice."</th></tr></table>";

how can i retain them??

Musa
  • 96,336
  • 17
  • 118
  • 137
Wafa
  • 53
  • 1
  • 1
  • 14

2 Answers2

0

You should use javascript to submit only parts of your page (1 of the forms or parts of a form) using ajax

jQuery is a very popular and easy to learn JavaScript library that makes such ajax calls very easy. Here is a tutorial.

An other solution:

If you don't want the page to reload and don't want to use JavaScript, set the target to a different page/frame like a (hidden) iframe. So the page with all the forms stays and the output of the submitted form can be passed to a different page (a new window, frame or an iframe).

If you don't care about any return you can just hide it. Here is a very basic example:

<form target="hiddenframe">
     <input type="submit"/>
</form>
<iframe name="hiddenframe" style="display:none"></iframe>
VDP
  • 6,340
  • 4
  • 31
  • 53
  • i have no idea about ajax..no other way?? – Wafa Oct 18 '12 at 06:35
  • You don't need to use javascript for your needs.. not if you don't want to. And seeing as your just starting out with PHP I'd suggest you get a better grasp on PHP before tackling javascript or any library like jquery made from javascript – chris Oct 18 '12 at 06:37
  • There is no way you can submit form parts without refreshing the page without using javascript! – VDP Oct 18 '12 at 06:38
  • Thank u for your tutorial. I will try to learn that and implement. thank a lot! – Wafa Oct 18 '12 at 06:40
  • Yes, true as that may be, the OP wants to stop it from rendering or reloading multiple times, and if such is the case, wants to retain the input values when the page reloads, never is mentioned anything indicating a need for ajax, though I can see where it could be deciphered as such – chris Oct 18 '12 at 06:40
  • "I can see where it could be deciphered as such" ?? – Wafa Oct 18 '12 at 06:44
0
$choice="";

    Print "<table border cellpadding=3>"; 
    print"<tr><th><form method=get action=''><select id=EMIRATE size=1 name='EMIRATE' class='comboBoo'>";

 print"</select><input type=hidden name='areas_hidden' value=<?php echo $_GET['AREAS'] ?>/>"
 print"</select><input type=hidden name='emerates_hidden' value=<?php echo $_GET['EMIRATE'] ?>/>"
</th>";

submit these value with both your forms submission action.then you will have both on your url.

Arun Killu
  • 13,581
  • 5
  • 34
  • 61