2

I have one big file (for right now) that is supposed to:

  • take a variable from a select widget in a form (jQuery)
  • submit the form asynchronously (jQuery)
  • return records from a database using the variable selected from the form (php)

The problem is, the variable never seems to reach the php code.

Can anyone tell me what I'm doing wrong, please?

My code:

(all on one page)

 <script type="text/javascript"> $(function() { $("select").change(function () { var str = ""; $("select option:selected").each(function () { str += $(this).text() + " "; }); $.post("index.php", { zip: str}, function(data){ $('result').text(str); }, "json" ); });//end select });//end function </script> <?php include_once ('../cons.php'); ?> <?php if (isset($_POST['zip'])){ $value = $_POST['zip']; }else{ $value = "nada"; } echo $value; //I only get "nada" ?> </head> <body> <div id="body"> <form id="findzip"><!-- jQuery will handle the form --> <select name="zip"> <option value="">Find your zip code</option> <?php //use php to pull the zip codes from the database $mysqli_zip = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s", mysqli_connect_error()); exit(); } $q_zip = "select distinct(zip) from mc m group by zip"; $r_zip = $mysqli_zip->query($q_zip); if ((!$r_zip) || ($r_zip == NULL)) { echo "no results "; } while($row_zip = $r_zip->fetch_array(MYSQLI_BOTH)) { echo "<option value='". addslashes($row_zip['zip']) . "'>" . addslashes($row_zip['zip']) . "</option>;\n"; }//end while ?> </select> </form> <!-- here's where the results go --> <result></result> <br/> </div> </body> </html> 
3
  • Try a print_r($_POST); to see what is coming through. Commented Nov 3, 2011 at 18:00
  • Hi, Amy, It returns Array(). I didn't realize it was returning an array, so maybe this is where the problem lies... Thanks! Commented Nov 3, 2011 at 18:37
  • No, that wasn't it. I call the $_POST object as $_POST['zip'], which should be correct, but it's empty. Commented Nov 3, 2011 at 18:40

2 Answers 2

2

Your script as written is echoing out a value like "12345". But your $.post is expecting it to be type json. When I removed the json type it worked perfectly for me.

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

3 Comments

"As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently."
I took out "json", but it still doesn't work for me. Patrick, can you please show me the $.post code you used?
I didn't save it, but all I did was remove the json string and the mysql pieces since I don't have the DB. Are you doing this in Firefox? Does the ajax request in firebug show that it completed with a 200 status?
0

Try putting an ID on it, like so:

<select name="zip" id="zip"> 

Then getting the value from it like so:

$("#zip").val(); 

5 Comments

Well, the value does work on the jQuery end--I can get the value to feed into an html tag:
Agh! I keep submitting my comment too early! Sorry. I can get the value to feed into an html tag with this line: $("result").text(str); The bug is somehow in the transmitting the value to the php.
Try going: var_dump($_POST); in your PHP and seeing if it's in there then.
Hehe, I used to always submit comments by accident :-P
var_dump($_POST); results in array(0) { } , and that value doesn't seem to change whether I've submitted my form or not.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.