Question

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??

Was it helpful?

Solution

$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.

OTHER TIPS

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top