0

I think I'm just having one of those weeks where whatever I try to get this work simply does not work. At one stage I did not think that I was far off solving this problem, but now I feel as if I'm further away from the solution.

I'm simply trying to populate a second dropdown menu based on input from the first. The complicated part is that the SQL query returns the values as A,B,C and I look to explode them (stripping the ,) as A B C (each being an option in my next dropdown menu).

I have made my query work but I am struggling with the post and AJax call in my php file

Here's my code:

my.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    $('#brigade').change(function() {   
        var t2choice = $('#t2choice').attr('value');
        var t3choice = $('#t3choice').val()
$.ajax({
            type: "POST",
            url: "ajax.php",
            data: "t2choice="+ t2choice,
            success: function(){
                $('form#submit').hide(function(){$('div.success').fadeIn();});
            }
        });

    });
</script>
<?php

print ("<br /><center><font size='+1'><a href='/adm/main.php'>Main</a> - Select ".$t2name."</font>");

    print ("<form method='post' action='' name='t2Form'>");
    print ("<br /><br /><select id='t2choice' name='t2choice' value='".$t2name."' onChange='return BrigadeListOnChange()'>'"); 
    print ("<option value='chooseOption'>Select a ".$t2name." to...</option>");   
    print ("</select>");

    print ("<select id='t3choice' name='t3choice'");
    print ("<option>Select t3</option>");
    print ("<input type='hidden' name='form' value='showt5objects'>");
    print ("&nbsp;&nbsp;<input type='submit' name='submit' value='Edit ".$t5name."s'>");
    print ("&nbsp;&nbsp;OR&nbsp;&nbsp;<input type='submit' name='submit' value='Add New'>");
    print ("<br /><br /><a href='javascript:history.go(-1)'>Cancel</a>");
    print ("</form>");

?>

ajax.php

<html>
<?php $t2=$_GET["t2choice"];
echo $t2;

//DB Connection
(mySQL connect information is here and confirmed working - no issues here)

//--------------------------------------------------------------------------------//
    $t3_query  = "SELECT tier3_short_names FROM tgdata_tier2 WHERE tier2_short_name = '" . $t2 . "' ORDER BY tier2_short_name ASC"; // query
    $t3_result = mysql_query($bde_query); ?>
    <select name="t3choice">
    <option>Select t3</option>
    <? 

    while ($t3_row = mysql_fetch_array($t3_result, MYSQL_ASSOC)) {
                                foreach ( explode( ',', $t3_row['tier3_short_names']) as $item)

echo '<option value="', $item, '">', $item,'</option>';

// while($row=mysql_fetch_array($t3_result)) {

    } 
    echo '</select></html>';
    ?>

The ajax file works when I manually route to ajax.php?t2choice=value - but I cannot get the second dropdown in my.php working :(

Any help would be greatly appreciated :)

2
Contributors
16
Replies
21
Views
4 Years
Discussion Span
Last Post by riahc3
Featured Replies
  • Why var t2choice = $('#t2choice').attr('value'); var t3choice = $('#t3choice').val(); ? They SHOULD both be the same but... Read More

  • Also, I personally dont know what "brigade" is. Does Firebug's console do anything when the event change is fired on brigade? Read More

  • > Apologies - thats my lack of simplifying my code. brigade should read t3choice > > I have adjusted both var to .val as you suggest that both should be the same. This is where I have been going wrong as I have been lifting snippets of code from various … Read More

  • Trust me, AJAX is a bitch so its normal the first time around this doesnt work :P You can see AJAX events (with POST and GET) using Firebug. In the network tab, whenever a change is made (in your case as thats the event being fired) it should show the … Read More

  • By your post, then yes, you should do change on t2. Whenever you change on t2, you modify t3. Read More

2

Why

var t2choice = $('#t2choice').attr('value');
var t3choice = $('#t3choice').val();

?

They SHOULD both be the same but...

2

Also, I personally dont know what "brigade" is. Does Firebug's console do anything when the event change is fired on brigade?

Edited by riahc3

0

Apologies - thats my lack of simplifying my code. brigade should read t3choice

I have adjusted both var to .val as you suggest that both should be the same. This is where I have been going wrong as I have been lifting snippets of code from various sources and piecing them together.

Firebug pointed out a missing "}" - and then a missing ")" - not sure where these are from, but having placed those no further errors are showing.

Upon changing the value of the first drop-down, the second menu is not displaying.

Thankyou very much for your assistance so far

Edited by JayJ

2

Apologies - thats my lack of simplifying my code. brigade should read t3choice

I have adjusted both var to .val as you suggest that both should be the same. This is where I have been going wrong as I have been lifting snippets of code from various sources and piecing them together.

Firebug pointed out a missing "}" - and then a missing ")" - not sure where these are from, but having placed those no further errors are showing.

Upon changing the value of the first drop-down, the second menu is not displaying.

Thankyou very much for your assistance so far

No problem, we all copy/paste....

Anyways, thats one important step: No errors such as missing ")" or "}".

But, you said it should be "t3choice". Personally that makes no sense; When you change t2choice, it should populate t3choice, not the other way around.

I apoligize if I am not understanding something correctly.

0

Yes, so the field t2choice specifies the top level that we want to retrieve data from. T3choice will be the dropdown for the next level in the cascade

e.g. Manufactorer (t2) - Model (t3)

So specifying the manufactorer will then display a list of models in the t3choice dropdown. Does that help make sense?

I'm happy that my code for the query works as it should. My issues at present seem to be:

  1. Getting my.php to post to ajax.php (it may already be doing that, but I have no way of testing it... I don't think?)

  2. Dropdown t3choice is not appearing on my.php at all

I do have a test environment set up if seeing it in operation would make more sense. There's no secracy about what it is I'm trying to do and I'm willing to show you via PM if required

I think my issues stem from the script I'm trying to use (this is the first time I have tried to use AJAX as you can probably tell).

2

Trust me, AJAX is a bitch so its normal the first time around this doesnt work :P

You can see AJAX events (with POST and GET) using Firebug. In the network tab, whenever a change is made (in your case as thats the event being fired) it should show the call. It should show you the reply, HTML reply, POST/GET etc.....

As a matter of fact, if you want, PM the address as I can see the page you call yourself and I can see what you pass and the reply. I cant see the PHP code (dontworry) just the reply and what is passed.

2

By your post, then yes, you should do change on t2. Whenever you change on t2, you modify t3.

0

No worries - I did send it by PM only a few minutes ago, but just seen your above reply. Thankyou for your help though thus far

2

OK, just looked at it....

Good news: Your parameters are being POSTed corrected, the page can be reached and it replies. Bad news: Its repling "".

Just in case do this test on your ajax.php page:

<?php 
$t2=$_GET["t2choice"];
echo $t2;
?>

This should just send back what you POSTed as a reply....

0

Here is a bit how you would see what I see.

You open up Firebug and refresh the page. You hit that tab (it should be network) and then change the dropdown to something else. As soon as it does that, you should see POST ajax.php appear. From there you can select Post, which will show the post variables passed. Reply which shows the reply the PHP page has sent back. And HTML which is like Reply but if the Reply has HTML code, it will be rendered as such.

http://img145.imageshack.us/img145/5854/44653098.png

I put a PNG just in case. Any questions just ask

2

I just noticed something when I was posting your code on here to reply to you:

$t2=$_GET["t2choice"];

Should be

$t2=$_POST["t2choice"];

Sorry about that.

The reason it works when you directly call http://yoursitee.com/ajax.php?hi=hello is because there you are passing it as a GET value and not as a POST value while in this AJAX call you are passing it as a POST value and your ajax.php expects a GET value. Try this out.

So you can do that or change in the ajax call to "type:GET" or put (not recomended)

$t2=$_REQUEST["t2choice"];

My personal recommendation is to change in the ajax.php to a $_POST

Edited by riahc3

Votes + Comments
Superb spot! Thankyou very much!
0

Certainly was one of my problems! Thankyou very much for your assistance - I have half of my script working now - the values are being passed.

Now I just need to make the <option></option> appear in my second menu now that the array is responding with ["1","2","3"].

I was advised not to put html in my ajax.php file for some reason. So now to figure out how to spit it out :)

2

Certainly was one of my problems! Thankyou very much for your assistance - I have half of my script working now - the values are being passed.

Now I just need to make the <option></option> appear in my second menu now that the array is responding with ["1","2","3"].

I was advised not to put html in my ajax.php file for some reason. So now to figure out how to spit it out :)

Step by step.....Thats good that now it replies.

I imagine that now that you know how Firebug/AJAX works you will have more knowledge on how to debug this. You could also download FirePHP to debug even further.

0

Yes and I have finally worked out how loops work in jquery!

Thankyou ever so much for all your help! Just the little pointers helped me along much quicker!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.