0

I'm hoping someone can help me with my design issue. I have index.php:

which contains a javascript piece of code:

<script type="text/javascript">
$(function() {
    $.getJSON('http://localhost/getData.php', function(data) {

    foo...

index.php also contains a form that updates the screen:

<form action="index.php" method="post" />
<input type="submit" name="action" value="update" /></p>

Now my problem is I need to pass some variables to getData.php to update the screen. getData works fine with the static info, but based upon the choices on the form, the data will change.

What's the best way for me to pass variables?

Or am I designing this incorrectly?

Thanks


My updated javascript looks like this:

<script type="text/javascript">
$(function() {
$.getJSON('http://localhost/getData.php', {
group_value: $('#group_id option:selected').val(),
updatedb_value: $('#update_db_id').val() 
}, function(data) {

Then my form looks like this:

<form id="myform" action="index.php" method="post" />
<select id="group_id" name="group">
<option value="0">Atlanta</option>
<option value="1">Chicago</option>
<option value="2">Los Angeles</option>
<option value="3">New York</option>
</select>
<input type="submit" name="action" value="Update" />
<input type="checkbox" id="update_db_id" name="update_db" value="1" />Update DB?<br />
</form>

No matter what I select or check, When ever I click the update button, the JS console shows that

GET http://localhost/getData.php?group_value=0&updatedb_value=1

Any suggestions?

3
  • I would suggest changing http://localhost/getData.php to simply /getData.php - localhost won't work if you deployed this as-is to a real server. That, and unless you're using JSONP, all your URLs should be relative to the root domain anyway. Commented Apr 30, 2012 at 0:48
  • Thanks for the suggestion, but same result. No matter what I select on the form, I get the following results: GET http://localhost/getData.php?group_value=0&updatedb_value=1. Commented Apr 30, 2012 at 13:17
  • Does anyone have any other suggestions? Thanks Commented May 2, 2012 at 0:36

2 Answers 2

2

You can use $(input_selector).val() to get the value of a form input, and you can pass it to getJSON as an object, like this:

$.getJSON('http://localhost/getData.php', {
    some_value: $(some_input).val(),
    other_value: $(other_input).val()
}, function(data) { ... });

You can also use serialize to pass all the form data:

$.getJSON('http://localhost/getData.php', $(form_selector).serialize(), function(data) { ... });
8
  • How would I access this from getData.php which is an all php script? Commented Apr 27, 2012 at 13:50
  • @Peaceful_Warrior, the data will be available in the $_GET variable. See php.net/manual/en/reserved.variables.get.php Commented Apr 27, 2012 at 16:33
  • I can't get this to work. I did this $.getJSON('http://localhost/getData.php', $('#myform').serialize(), function(data) { ... }); then this <form id="myform" action="index.php" method="post" /> <input type="submit" name="action" value="update" /> Then, when I go to getData.php & do this if (isset($_GET["action"])) { do stuff } nothing happens. What am I doing wrong? Commented Apr 28, 2012 at 14:52
  • Look into the value of $('#myform').serialize() in your js console. It's possible serialize doesn't take into account the value of the submit button, I'm not sure. If it doesn't, you'll have to pass that in manually using the first method I posted. Commented Apr 29, 2012 at 4:17
  • Ben this isn't working. I'm going to put the full code of what I have & see if you can tell me what I'm doing wrong. Commented Apr 29, 2012 at 15:58
0

Hmm. Look very closely at this line:

<form id="myform" action="index.php" method="post" />

Oops. You self-closed your form. That may be causing issues with how the input/select elements update. Try changing it to:

<form id="myform" action="index.php" method="post">

You also don't actually need the complex selector on the select element. This will also get you the selected value:

$('#group_id').val()
1
  • Thanks, I've taken all the suggestions you guys have provided & still can't seem to get it to work. I still get GET http://localhost/getData.php?group_value=0&updatedb_value=1 in JS Console. Commented May 2, 2012 at 14:02

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.