2

i have been searching a solution for days and i have tried ajax/jquery methods posted online but it just wouldnt work. I have a drop down list which gets its value from the database. On selecting any value apart from "Select", i want to display a value which is called upon by a php file:

here's my code for the form:

         <tr>  
        <fieldset id="Date">
          <td class="select"><label><span class="text_9">Date:</span></label></td>
          <td><select name="date" id="date">
          <option value="">Select</option>
          <?php include_once "selectdate.php"?></td>
          </select>
     </tr>
        </fieldset>
   </table>

and here's the php to run on selection of the drop down (called retrieve.php)

<?php
include_once "connect.php";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); 

$form=$_GET;
$trade=$form['tradetype'];
$metal=$form['metal'];
$amount=$form['amount'];
$date=$form['date'];

$stmt = $conn->query("SELECT Discount FROM Contracts WHERE Trade='$trade' AND Metal='$metal' AND Amount='$amount' AND ExpiryDate='$date'");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
   echo ($row['Discount']); 
} 
?>

As you can see, the php to be run uses the value from multiple form elements...

I am very new to jquery/ajax... any help is appreciated as i want the result to be displayed on the same page as the form is. Thank you!

2
  • Where are you sending ajax? What is $form? You are open to SQL injections with this code. Use parameterized prepared statements. php.net/manual/en/pdo.prepared-statements.php Commented Nov 2, 2015 at 4:16
  • The tr and fieldset open and close tags do not match. Just FYI. Commented Nov 2, 2015 at 5:09

1 Answer 1

1

If you want to get data from another file to a select, you should add as option. Plain text inside select will not help you out. So wrap the vales with <option></option>

So change this line,

echo "<option>{$row['Discount']}</option>"; 

If you want to give values,

 echo "<option value='{$row['Discount']}'>{$row['Discount']}</option>"; 

EDIT

Now onchange of deopdown, date call ajax to do next stuff.

$(document).on("change","#date",function() {
     var tradetype = //capture the value here;
     var metal = //capture the value here;
     var amount = //capture the value here;
     $.ajax({
          url:"path/filename.php",
          data:{date:$(this).val(),"tradetype":tradetype,"metal":metal,"amount":amount},
          type: "POST",
          success: function(data){
               alert(data);//this will alert what you have echoed on php file
          }
     });

});
Sign up to request clarification or add additional context in comments.

17 Comments

Need to give it a value as well.
He has included a file and echoed the values from db inside select. He is just echoing the value in php file. He shud wrap with <option> tag.
I was about to add that in answer. But you asked for clarification.
Yea, answer was very vague to begin with. OP still has multiple issues with the current approach.
Yes, i will try changing my answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.