1

This is my first dabbling in Ajax and I'm confused.

Problem: JS variable doesn't seem to get passed to php through Ajax.

I get this notice:

Notice: Undefined index: text in C:\xampp\htdocs\Website\ref_files\delete.php on line 31

Line 31 is: $name = $_POST['text'];

So the problem seems to be that 'text' is not being passed to the php as far as I can tell?

Both the JS and the PHP are in delete.php which is included in WhatsNew.php.

I get Response: displayed on the page, as well as an alert saying "success".

There is a value in 'text', I have tested it with alert(text).

JavaScript + Ajax

<script>

var text = $('#title').text()

 $.ajax({
         url: "WhatsNew.php",
         type: "post",
         cache: "false",
         data: text,
         success: function(){
             alert("success");
         },
         error: function(){
             alert("failure");
         }
     });
</script>

PHP

<?php
$name = $_POST['text'];
echo "Response: " . $name;
 ?>

If more information is required it will be posted beyond this point.

4 Answers 4

2

You need to change data to:

data: {text: something},

and also change the name of the variable in js, as the compiler won't know which text to take, like:

var something = $('#title').text();

Please do comment if this does not work!

Try this in php on a different file with some response in success:

<?php 
echo "hello"; 
$name = $_POST['text']; 
echo "<label id='1'>Response: " . $name . "</label>"; 
?> 
Sign up to request clarification or add additional context in comments.

7 Comments

Ok so I've made the changes but I still get the same error, undefined 'something' now on $name = $_POST['something']. @SML
the $_POST['text'] will be the variable stated at the start of your data data: {text (this one):something}.
Success alert is still there but same error. (with namechange obviously)
Full code added. I just expected the ajax code to run when the 'delete.php' code was required by the previous file
also remember to add in a response to your success to make changes to the page!
|
0

you need to change

data: text,

to

data: {text:text},

1 Comment

I still get: Notice: Undefined index: text in C:\xampp\htdocs\Website\ref_files\delete.php on line 30
0

You are on track, just one modification:

data: {text:text},

You need to send text as key to posted data.

1 Comment

I still get the same error using this, any ideas what else I'm doing wrong?
0

You need to wait for the document to be ready before trying to read $('#title').text().

Just wrap everything in $(function() {...}), (and data:{text:text} as others have pointed out).

$(function() {
    var text = $('#title').text();
    $.ajax({
        url: "WhatsNew.php",
        type: "post",
        cache: "false",
        data: {text:text},
        success: function(){
            alert("success");
        },
        error: function(){
            alert("failure");
        }
     });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.