简体   繁体   中英

Ajax- fill multiple textboxes for php

I have a html page with a drop down menu, The menu works and onchange calls a function popBox() that too works. Within the function i am using ajax to post the value of the drop down menu into php where it selects form the db. I wish to fill the textboxes in the form "DetailsForm" with the information selected. I currently fill no text boxes and the alert (msg) displays the whole html side of the page in and alert box. Could somebody please help me with my problem. I have tried multiple different variation of ajax and jquery to perform this and after 15hrs on the same function i am starting to get slight frustrated to say the least. Thanks in advance for any help, i do appreciate it.

Here is my code: HTML

    <head>
    <link href="../UserTemplate.css" rel="stylesheet" type="text/css"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <!-- TemplateBeginEditable name="doctitle" -->
    <title>Tours</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js">  </script>

    <script type="text/javascript">
    function popBox(str)
    {
        $.ajax({
      type: "PopulateBoxes.php",
      data: { dat: str}
      }).done(function( msg ) { //you can also use success
       alert( "Data Saved: " + msg );
      });
     //document.getElementById("txt_Duration").value = <?php Print($Duration); ?>;
     //document.getElementById("txt_Vessel_Name").value = <?php Print($Vessel); ?>;
     //document.getElementById("txt_Location").value = <?php Print($Location); ?>;
     //document.getElementById("txt_Available").value = <?php Print($Available); ?>;
     //document.getElementById("txt_Price").value = <?php Print($Price); ?>;
    }
    </script>
    </head> 
    <body>
    <div id="MainDiv">
        <div id="Header">
            <div id="Logo"><img src="../Scotia Sea Life Logo.png" width="150px"  height="110px" alt="Company Logo"/></div>
             <div id="NavBar"><ul>
                        <a href="homepage.php">Home</a> <a href="SelectStudentScore.php">Tours</a> <a href="AdditionPageMain.php">About</a> <a href="SubtractionPageMain.php">Donate</a> <a href="Devisionmain.php">Account</a>
               </ul>
         </div>     
           <div id="Title">Tours</div>
      </div>
            <div id="Content">
              <div id="Search">
                <div id="SearchDiv">
               <form id="SelectTourForm" style="margin:5px;">
                           <table border="0" align="center" width="100%">
                                        <tr>
                                        <td>
                                        <label style="color:#FFF; font:Georgia, 'Times New  Roman', Times, serif; font-size:20px; margin-left:10px; margin-top:25px">Select Tours  Details</label></td>
                                        </tr>
                                        <tr>
                                          <td><select name="lst_MonthDrop"  style="background-color:#FF9933; color:#FFF; border:none; margin-top:10px; margin- left:10px;" onchange="popBox(this.value);">
                                                <option>Please Select</option>
                                                 <?php 
                                                 include 'populatedrodown.php';
                                                 foreach ( $results as $option ) : ?>
                                                      <option value="<?php echo $option- >Date; ?>"><?php echo $option->Date; ?></option>
                                                 <?php endforeach; ?>
                                            </select>
                                            <input type="submit" name="btn_TourSearch" id="btn_TourSearch" value="Search" style="background:#009300; border-radius:5px; border-color:#009300; color:#FFF;margin-left:5px;" /></td>
                                        </tr>
                                        <tr> 
                                          <td></td>
                                        </tr>      
                        </table>

                        <p>&nbsp;</p>
              </form>

            </div>
          </div>
          <div id="DetailsDiv">
            <div id="DetailsContent">
                <form id="DetailsForm" >
                    <table border="0" align="center" width="100%">
                    <tr><td><label style="color:#FFF; font-size:14px;">Tour ID</label>    <input type="text" id="Tour_ID" />    </td></tr>
                    <tr><td><label>Duration</label> <input type="text" id="txt_Duration" />     </td></tr>
                    <tr><td><label>Vessel Name</label> <input type="text"    id="txt_Vessel_Name"/> </td></tr>
                    <tr><td><label>Location</label> <input type="text" id="txt_Location" />   </td></tr>
                    <tr><td><label>Date</label> <input type="text" id="txt_Date" />        </td></tr>
                    <tr><td><label>Available</label> <input type="text" id="txt_Available" />  </td></tr> 
                    <tr><td><label>Price</label>  <input type="text" id="txt_Price" />     </td></tr>         
                    </table>
                </form>
            </div>
          </div>













        </div>
        <div id="Footer">
        <div id="FooterLinks"></div>
    </div>
</div>
</body>
</html>

PHP

    <?php
    $q = $_POST['dat'];

    $mysql_db_hostname = "localhost"; 
    $mysql_db_user = "root"; 
    $mysql_db_password = "pwd"; 
    $mysql_db_database = "db";

    $con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or   die("Could not connect database");
    mysql_select_db($mysql_db_database, $con) or die("Could not select database"); 

    $sql="SELECT * FROM Tour WHERE Date = '".$q."'";

    $result = mysqli_query($con,$sql);


    while($row = mysqli_fetch_array($result))
      {

     $Duration = $row['Duration'] ;
     $Vessel = $row['Vessel_Name'] ;
     $Location = $row['Location'] ;
     $Available = $row['Available'];
     $Price = $row['Price'];
      }

    mysqli_close($con);
    ?>

Try to modify you JS code similar to this:

 function popBox(selectValue) {
   $.ajax({
     type: 'POST',    
     url: "PopulateBoxes.php",
     data: { dat: selectedValue },
     success: function(serverResponse) {
       // after success request server should return response with data 
       // that will be passed to this callback function as parameter
       // and you can use it to fill text boxes:
       $('#txt_Duration').val(serverResponse.duration);
     }
   });
 }

Also you should modify your PHP code to return data in JSON:

// At the end you should return selected array. For example:    
echo json_encode($dataArray); exit;

As you are using $_POST in your PHP code, you would need to edit the ajax call script. Type is either GET or POST and page address comes in to the url attribute.

$.ajax({
  type: 'POST',
  url: "PopulateBoxes.php",
  data: { dat: str}
  }).done(function( msg ) { //you can also use success
   alert( "Data Saved: " + msg );
  });     
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM