www.webdeveloper.com
Results 1 to 10 of 10

Thread: problem with my Reservation form

  1. #1
    Join Date
    Dec 2011
    Posts
    6

    problem with my Reservation form

    Hello all,
    Please execuse my English

    I had a form with 2 dropdownlist one select airport and the other select zipcode. I need to get the price from mysql based on user selection.
    when I used one dropdownlist I can get the data. my problem is how to pass the 2 dropdownlist variable to get the only the price from the table, based on user selection from airport drop and zip

    My table goes like this
    ID : Airport : ZipCode : Price
    1 : Dal : 75062 :70
    2 : Dal : 75061 :80
    3 : DFW : 75062 :65
    4 : DFW : 75062 :80

    the following code work fine with one drop down list its just work from the airport dropdown, I need to include the second one in the query without submitting the form.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function showUser(str)
    {
    if (str=="")
      {
      document.getElementById("txtHint").innerHTML="";
      return;
      }
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","get_user.php?q="+str,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    
    <form>
      <p>
        <select name="users" onChange="showUser(this.value)">
          <option value="">Select airport</option>
          <option value="DFW">DFW</option>
          <option value="DAL">DAL</option>
                </select>
    </p>
      <p>
        <label>
        <select name="zip" id="zip">
          <option>select zip</option>
          <option>75062</option>
          <option>75061</option>
          <option>75030</option>
        </select>
        </label>
    </p>
    </form>
    <br />
    <div id="txtHint"><b>Price will go there</b></div>
    <br />
    </body>
    </html>
    Code:
    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("ejbooking", $con);
    
    $sql="SELECT * FROM price WHERE airpot= '".$q."'";
    /* //$sql="SELECT * FROM price WHERE airpot = '".$q."'";
     */
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>airport</th>
    <th>zipcode</th>
    <th>price</th>
    
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['airport'] . "</td>";
      echo "<td>" . $row['zipcode'] . "</td>";
      echo "<td>" . $row['price'] . "</td>";
      
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?>
    I had been stacking for one month in this form could any body here help me.

    Thanks

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,408
    Just add an AND logical operator to your WHERE clause.
    Code:
    . . . WHERE airport = 'foo' AND zipcode = 'bar'
    PS: You should probably look at mysql_real_escape_string() in order to avoid SQL injection issues.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    6
    Many thanks for your response and help. But that didn't work for me. I have two dropdown element of for airport and other for zip code I need the price to appear in the same page before submitting the form.

    Thank you for the link, its useful.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    You have, at first, to send the two selected values with your Ajax function showUser(str) which can not now to be call with an unique argument str !

    After reading the two values (for example, var airportSlc=document.form[0].users.value; for the first), this function without argument (called with an onsubmit button) as to make tests and finally to send a get with the two values with a complete get url such as the following one...
    Code:
    xmlhttp.open("GET","get_user.php?=rpr"+airPortSlc+"&zip="+zipSlc,true);
    Additionally, add a DOCTYPE at your page, change users with airport, remove the useless upper case letter in event (for example onchange="getPrice()" on the submit button) and add values to the zip drop-down list options.
    Last edited by 007Julien; 12-17-2011 at 08:50 AM.

  5. #5
    Join Date
    Dec 2011
    Posts
    6
    007Julien Thank you for your help. But I failed to do that.
    in the attachment what I have so far. I am pulling my hair, can you please do that for me

    Many Thanks
    Attached Files Attached Files

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268

    resolved

    There two files ajax.htm (php is useless here) and get_price.php which work together
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="fr">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
    <meta name="generator" content="PSPad editor, www.pspad.com">
    <title>Resevations</title>
    <style type="text/css">
    body {font:13px/17px "Lucida Handwriting","Comic Sans MS",cursive;color:#300;text-align:center;background-color:#ffc}
    h1 {margin:20px;font-size:24px;}
    form,div {display:block;width:500px;margin:0 auto;text-align:left}
    input {width:400px;font:13px/17px "Lucida Handwriting","Comic Sans MS",cursive;color:#300;border:0;background-color:#ffc}
    
    </style>
    <script type="text/javascript">
    function getPrice() {
    	//Read the values
    	var airportSlc = document.forms[0].airport.value;
    	var zipSlc = document.forms[0].zip.value;
    	// Test the values
    	if (!airportSlc) {document.getElementById("price").value ="Please, Select an airport !";return;}
      if (!zipSlc) {document.getElementById("price").value = "Please, Select a zip !";return;}
      // Send request
    	if (window.XMLHttpRequest)
    	  {// code for IE7+, Firefox, Chrome, Opera, Safari
    	  xmlhttp=new XMLHttpRequest();
    	  }
    	else
    	  {// code for IE6, IE5
    	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	  }
    	xmlhttp.onreadystatechange=function()
    	  {
    	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	    {
    	    alert(xmlhttp.responseText)
    	    document.getElementById("price").value=xmlhttp.responseText;
    	    }
    	  }
    	xmlhttp.open("GET","get_price.php?arp="+airportSlc+"&zip="+zipSlc,true);
    	xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <form>
    <h1>Reservations</h1>
    <p>
    	<select name="airport" onchange="getPrice()">
    		<option value="">Select a airport :</option>
    		<option value="DFW">Dalls-Fort Worth</option>
    		<option value="DAL">Dallas Lowe Fields</option>
    	</select>
    </p>
      <p>
        <select name="zip" onChange="getPrice()">
          <option value="">select zip</option>
          <option value="75006">75006</option>
          <option value="75061">75061</option>
          <option value="75030">75030</option>
        </select>
    	</p>
    	<p>
    		<input id="price" type="text" value="Please, Select an airport and a zip">
    	</p>	
      <p>
    </form>
    </body>
    </html>
    PHP Code:
    <?php
      
        header
    ("Cache-Control: no-cache, must-revalidate"); 
        
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        
    header("Content-Type:text/html;charset=windows-1252");
      
        if (!empty(
    $_GET) && isset($_GET["arp"]) && isset($_GET["zip"])) {
            
            
    $arp=htmlentities($_GET["arp"]);
            
    $zip=htmlentities($_GET["zip"]);    

    /*
        $con = mysql_connect('localhost', 'root', '');
            if (!$con)  die('Could not connect: ' . mysql_error());
        $dbs = mysql_select_db("ejbooking", $con);
            if (!$dbs) die('Can\'t use ejbooking : ' . mysql_error());

        $sql = 'SELECT price FROM price WHERE zipcode = "'.$zip.'" and airport= "'.$arp.'"';
        $result = mysql_query($sql);

            echo "<table border='1'>
            ...
            echo "</table>";
            mysql_close($con);
    */        
            // Only for test
            
    echo "The server received effectively $arp and zipcode $zip !";    
        }
        else echo 
    "No GET request on the server !";                
    ?>

  7. #7
    Join Date
    Dec 2011
    Posts
    6
    Thank you so much for your help and your time.

    Ajax.htm and get_user.php you provide are not working nothing can be pulled out from the database, I remove the comments from the query section and nothing happened.

    I tried to pass the function onsubmit too but its not working.

    Many Thanks

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,268
    I have only rewrite the sql request with the php variables $zip and $arp, but was not able to test it without database... Both pages work perfectly with the comments. It should be easy to finalize the request with the alert(xmlhttp.responseText) witch display the PHP or SQL error messages if any.
    The button on submit is useless with Ajax, it is now only useful to see the form of the GET url.
    Good Luck !
    Last edited by 007Julien; 12-17-2011 at 07:25 PM.

  9. #9
    Join Date
    Dec 2011
    Posts
    6
    Thank you,
    I am still can not test it. when I test it with comments it work fine, when using database its not working. you can find attached database in earlier post.

    Many thanks

  10. #10
    Join Date
    Dec 2011
    Posts
    6
    Hi,
    Thank you so much for your help, The problem now solved. I made minor change to the php code specially the query, here is the code working perfect with your Ajax file.

    Code:
    <?php
     $arp=$_GET["arp"];
     $zip=$_GET["zip"];
     
     
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    $db_selected = mysql_select_db('ejbooking', $con);
    if (!$db_selected) {
        die ('Can\'t use DB : ' . mysql_error());
    }
    
    
    $sql="SELECT `price` FROM `price` WHERE airport = '".$arp."' AND zipcode=".$zip." ;";
    
    $result = mysql_query($sql);
    if(!$result)
       {
    	  die('check your query :'.mysql_error());
    	}
      while($row = mysql_fetch_array($result)) {
      	
      echo $row['price'] ;
        
      }
    
    mysql_close($con);
    ?>
    I am still working in the form I hope I can finalize the rest of it.

    Many thanks for your help again

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles