0

I am trying to populate an initial customer select box with results from PDO MySql via PHP. Then I would like the second contact select box to update with additional information related to what was chosen in the first box. I can't get the second script to work. I think the problem is in my ajax script because the PHP scripts work fine when ran on there own.

The Primary Script

 <html>
<head>
    <script type="text/javascript" src="js/jquery-1.11.0.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            $("#contact").change(function(){
                 var cid = $("#cid").val();
                 $.ajax({
                    type:"post",
                    url:"contact.php",
                    data:"cid="+cid,
                    success: function(data) {
                      $("#contact").html(data);
                    }
                 });
            });
       });
    </script>
 </head>
 <body>

    Campaign :
    <select name="customer" id="customer">
      <option>-Select a Customer-</option>
    <?php 
    include ("function.php");
  include("connect.php");
    $id = $_SESSION['profile']['id']; 
   foreach($db->query("SELECT * FROM customers WHERE pid = '$id'") as $row) {
        echo "<option value=" . $row['id'] . ">" . $row['name'] . "</option>";
}
        ?>
    </select>



    <select name="contact" id="contact">
        <option>-Select a Contact-</option>
    </select>
  </body>
</html>

The Contact script

    include("connect.php");
$cid = $_POST["cid"];
foreach($db->query("SELECT * FROM contact WHERE cid = '$cid'") as $row) {
    echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';

3 Answers 3

1

Maybe your second function should start on #customer change

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

Comments

0

I see you used the contact select in ajax not customer as you described. However the code you wrote, you used the contact selector with change event, But the contact select box contain only one value, How can it change ??

<select name="contact" id="contact">
    <option>-Select a Contact-</option>
</select>

The previous select should has more than option to can change. Or I think you mean the #customer instead contact as following:-

 $("#customer").change(function(){
                 //  your code;
            });

Comments

0

Why not just encode a JSON response with the ids and names?

foreach($db->query("SELECT * FROM contact WHERE cid = '$cid'") as $row) {
    $arr[] = array("id" => $row['id'], "name" => $row['name']);
}
echo json_encode($arr);

Then in your ajax response, you could do

$(document).ready(function () {
    $("#customer").change(function () {
        var cid = $("#customer").val();
        $.ajax({
            type: "post",
            url: "contact.php",
            data: {cid: cid},
            success: function (data) {
                var options = [];
                $.each(data, function () {
                    options.push('<option value="' + this.id + '">' + this.name + '</option>');
                });
                $("#contact").html(options.join(""));
            }
        });
    });
});

14 Comments

I changed to the scrip you suggested and now I don't even get the <option>-Select a Contact-</option> all I see is a down arrow
Disregard what I just said I get nothing in the contact drop-down just the hard coded stuff.
I just noticed something else wrong with your code. I will update answer
Open your browser network tab in developer tools and tell me what the json response looks like when you change one of the dropdowns
contact.php /notaryaccounting POST 200 OK text/html jquery-1.11.0.js:9666 Script 379 B 4 B 19 ms 19 ms
|

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.