2

I know this question has been asked a lot and this is going to get flagged as duplicate, but I need the code because I can't get my head around any of it.
I have a variable x in my js file. I want that in my php file. Here's the messed up code that I have:
index.js-

var x=5;
  $.ajax({
  type: 'POST',
  url: 'form.php',
  data: {'variable': x },
});  

form.php-

<?php $myval = $_POST['x'];
echo $myval;?>

Also, do I need to connect with the server first or something for the ajax call? Thanks in advance.

1
  • 1
    You don't need the ?> and are probably better off without it, might get new line of returned value as is. Commented Feb 4, 2016 at 13:58

3 Answers 3

5

You're adding this POST body

['variable' => 5]

Why are you then requesting $_POST['x'];?
The index x is undefined and will throw a notice/error.

Something useful you can do (during development only), when you're unsure what is accessible in you're PHP code is dumping the required variable:

<?php 
   var_dump($_POST);
?>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the update @chris85. I'm still new to the formatting within SX.
Might want to look at meta.stackoverflow.com/questions/251361/… for formatting options in the future. It was pretty well formatted already.
Hello people. The 'x' in there was a mistake. I'd written the correct code before and it still showed an "undefined index: variable" error.
Please verify that the correct PHP file is requested by the Ajax request. If so, check the available indexes with var_dump($_POST);
doesn't work. It shows an array of the login data thats present in the same form.php file though. Maybe the stuff isnt referencing properly.
|
4

In Ajax data you have sent key:value. So in PHP file you can access it by $_POST['key'].

Here your key is variable and value is x which is 5 So you can access it by $_POST['variable']

Write it as below:-

<?php 
$myval = $_POST['variable'];
echo $myval; // output will be 5
?>

Hope it will help you :)

7 Comments

Oh sorry about that. I did use this code and it still shows the same error. Undefined index: variable. The code looks alright though. Everything is on my localhost. I have the src tag referring to the js file alright too. Can't figure out what might be the problem
Write this line and check output :- echo '<pre>'; print_r($_POST);
What is your output?
doesn't work. It shows an array of the login data thats present in the same form.php file though. Maybe the stuff isnt referencing properly.
Notice: Undefined index: variable in C:\xampp\htdocs\form.php on line 100 array(3) { ["submitLogin"]=> string(0) "" ["uname"]=> string(5) "navya" ["password"]=> string(8) "wrvwrvwr" }
|
1

This ajax call is like this form submit

<form action="form.php" method="POST"> 
<input type="text" name="variable" value="5">
<input type="submit" value="Submit">
</form>

Then in your form.php you will do:

<?php 
myval = $_POST['variable'];
echo $myval; // output will be 5
?>

Comments

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.