3

I am trying to pass a variable from jQuery to a PHP file and I am having a difficulty in doing it.

Here is a simple HTML file of "send.html".

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
  $(document).ready(function(){
    $("button").click(function(){
      $.ajax({
        type: 'post',
        url: 'receive.php',
        data: {
          message : "Hello World!",
        },
        success: function(data) {
          //console.log(data);
          alert(data);
        }
      });
    });
  });
</script>
</head>
<body>
<button>Click here</button>
</body>
</html>

And I have a PHP file of "receive.php".

<?php
  $message = $_POST['message'];
  echo $message.'<br />';
?>

When I click the button on send.html, I get the correct alert saying "Hello World!". But when I call the receive.php file, I get the following error message saying:

Notice: Undefined index: message in receive.php on line 3

Does anyone know the solution to this problem?

Thanks.

6
  • What do you mean by " I call the receive.php file"? Commented Jan 27, 2014 at 6:58
  • As you can see it from HTML file, I am not using a form. When I type the URL of receive.php in my brower (this what I mean as calling receive.php), I want the message variable to be echoed out. Commented Jan 27, 2014 at 7:28
  • 1
    @user3239379 I just understood what you meant. You see when you make a post request to receive.php with the message variable. The receive.php script gets the message and echos out "hello world" correctly which is why you see the alert box when you open send.html in the browser and click on the button. However when you just open receive.php in the browser, the php file has no way of knowing the message variable as it is not set when you just "call" receive.php Commented Jan 27, 2014 at 7:39
  • If you want $message to persist even when you directly call the receive.php file. You want to store the string that you send to the php file in something called a session variable. So you should set up session variable like $_SESSION['message']=$message. This variable persists through out the session.The link should get you started in the right direction. Hope it helps. Commented Jan 27, 2014 at 7:42
  • You mean this ajax method only works when you submit a form, right? Commented Jan 27, 2014 at 7:46

3 Answers 3

1

$_POST['message'] this will work in your receive.php only when you go to your php file by clicking the submit button as you have used ajax for calling your php file. But if you will call receive.php file without clicking submit button it will give error as nothing is posted on your php file. So for avoiding this error use isset() function:

<?php
 if(isset($_POST['message'])){
   $message = $_POST['message'];
   echo $message.'<br />';
 } else {
   echo "message not set";
 }
?>

For more Details on isset() see https://www.php.net/isset

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

2 Comments

This means that I need to use a form which will call receive.php when the form will be submitted. I want to avoid a form since it will call receive.php automatically. I want a user to call receive.php when he or she is ready. Is this possible?
If you want to caal recieve.php directly without form submit then yopu have to usee isset() to check the message variable else you will get error.
0

Remove , at the end of value in data parameter of jquery

$(document).ready(function(){
    $("button").click(function(){
      $.ajax({
        type: 'post',
        url: 'receive.php',
        data: {message : "Hello World!"},
        success: function(data) {
          //console.log(data);
          alert(data);
        }
      });
    });
  });

Comments

0

The ajax request should be like:

$.ajax({
  type: "POST",
  url: "receive.php",
  data: { message: "Hello World"}
 })
.done(function(data) {
  alert(data);
 })

Try to use the done construct instead of success as it is deprecated now. As far as the receive.php file is concerned, make sure the path is set correctly in the url parameter and best practice should be in the receive.php file:

<?php
  $message = "NULL";
  if(isset($_POST['message']))
      $message = $_POST['message'];
  echo $message.'<br />'; 
?>

This way you won't get a Undefined index message error when you run pull up receive.php file and would also help you debug it to some extent. Hope it helps.

3 Comments

Ok. So when you run the send.html,you are not seeing an alert saying "hello World"?
Yes, I am seeing the alert with "Hello World!". But when I access to receive.php, $message is still not set.
Please see my comments below the question, I think I got what you are saying.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.