pass a JavaScript variable to php via Ajax

-2

I try to pass a JavaScript variable to php via Ajax so then I can make sql query. I call function votepick(id ) onchange event. However not working and get error Uncaught ReferenceError: $ is not defined.

html code:

</tr><select onchange="votepick('1')" name="vote1" id="1"> <option selected disabled>Vote here</option> <option value="0">00 vote</option> <option value="1">01 vote</option> <option value="2">02 vote</option> <option value="3">03 vote</option> <option value="4">04 vote </option> <option value="5">05 vote </option> <option value="6">06 vote</option> <option value="7">07 vote</option> <option value="8">08 vote </option> <option value="9">09 vote</option> <option value="10">10 vote</option> </select> </td> <td><?php include 'phpexample.php';?></td>

javaScript file:

function votepick(id){ var x = document.getElementById(id).value; //alert(x); $.ajax({ type: "POST", url: 'phpexample.php', data: {voteid: x }, success: function(data) { alert("success! X:" + data); }
}); }

php file:

<?php if (isset($_GET['voteid'])) { $x = $_GET['voteid']; echo $x; echo "ok"; }else{ echo 'no variable received'; } ?>

csgk

Posted 2014-05-03T13:08:35.813

Reputation: 1

Question was closed 2014-05-04T23:15:57.913

After var x = document.getElementById(id).value (before the AJAX call) does X have the expected value? – Ƭᴇcʜιᴇ007 – 2014-05-03T14:29:54.487

Yes, i have checked with alert. – csgk – 2014-05-03T14:44:40.617

Answers

1

I assume you're getting this error in your JavaScript code.

This is because you're using a jQuery function but haven't included the jQuery library.

You can include it by putting the following line in the <head> section of your HTML file:

<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.min.js"></script>

Louis Matthijssen

Posted 2014-05-03T13:08:35.813

Reputation: 442

Thanks,however no variable received... any idea? – csgk – 2014-05-03T14:07:49.317

In your JavaScript you send data using POST and in PHP you retrieve data using GET. What's the difference? So, you should change $_GET to $_POST in your PHP file.

– Louis Matthijssen – 2014-05-03T14:11:24.197

Yes,thanks again, but nothing,no variable received again.... – csgk – 2014-05-03T14:24:29.327

Well, you're not showing the returned data anywhere, so how can you be sure? You can change alert("success!"); to alert("success! X: " + data); to display the data the PHP script is returning. – Louis Matthijssen – 2014-05-03T14:44:29.457

Return the correct value. – csgk – 2014-05-03T14:49:58.120

After echo $x; i put echo"ok"; ,and dispayed it in the alert box with the "success! X: ". however $x not printed. – csgk – 2014-05-03T14:54:19.273

Was it working before adding echo "ok":? Could you update your question with all your current code? – Louis Matthijssen – 2014-05-03T15:05:21.593

i have update my code. i get alert with "success! X: <my value select>ok". However, $x doesn't print where i "call" php file in html. I write just an ok message in php and it print it ok, but $x not printed, just print not variable received. Tnanks again! – csgk – 2014-05-03T15:18:48.687

You mean you want to place the returned value somewhere in your HTML instead of an alert box? – Louis Matthijssen – 2014-05-03T15:22:18.090

Not exactly. I want to print a value in the place where i "call" php file (in HTML). in php file i want to get the value selected (with javaScript) do queries and then print the output value where i "call" php file.I hope it is clearly enough my explaination. – csgk – 2014-05-03T15:28:09.563

Oh, you want the output on the place of <?php include 'phpexample.php';?>? – Louis Matthijssen – 2014-05-03T15:33:38.453

Yes. As i said before when i wrote only an echo "ok"; in php file, ok print it in this place,but not $x. If there is a smarter way please suggest me. – csgk – 2014-05-03T15:36:03.050

1I don't think you fully understand what AJAX does. Basically, you call the PHP script (optionally with some data, like the selected vote count in your case), the PHP script will process this request (you can do anything here, like use the vote count to insert x votes in your database) and return some data (for example "vote succesful" using echo) to your JavaScript. This return value is stored in the data var in JavaScript. You can use JavaScript to display it in your HTML page. The PHP script has executed, so the data is gone. By including it you won't get the same data. – Louis Matthijssen – 2014-05-03T15:42:50.097

Ohhhhh,i'm new in Ajax. I don't know that working like that.Now i have clearly understand. Many thanks. i spend a whole day trying this. – csgk – 2014-05-03T16:03:04.370

Nice that you understand it now. Have fun programming! – Louis Matthijssen – 2014-05-03T16:09:08.560