2

I am creating a site where the user fills out a form, has that data sent to a php script and display result in a div and have been struggling to complete it for a while now. Below is the code i have created:

the button activates this function:

function callFunc()
{
    var testVar = "Example 01";
    $.ajax(
    {
        url: "public/includes/userCreate.php",
        type: "POST",     
        data: testVar,  
        success: function(html) 
        {             
              $('#web_Content').html(html);
        }      
    });     
};

The PHP file is this:

<?php
    $test = $_POST['testVar'];
    echo $test;
?>

For some reason though i doesnt work. I mean it doesnt inject the value of the echo command into the div, If however i take out the variable data from the PHP and just have it echo a simple element, it does work and injects the echo value into the div, Below is the code that will run:

<?php
    $test = $_POST['testVar'];
    echo "<h3>User ???? Created.</h3>";
?>

I think it is because i am not sending data correctly, Could anyone tell me the correct way to send the data to the PHP script and also, how to send more than one variable?

1
  • Have you tried data: testVar.serialize(),? Commented Apr 11, 2011 at 20:39

2 Answers 2

3

you need data: { testVar: "Example 01"} it is a key value pair. This will generate a query string parameter like testVar=Example%2001

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

Comments

2

The correct way to send data via the AJAX function is with either an object or string. Examples:

var pData = "testVar=Example.";
$.ajax({
/* Other ajax params */
data: pData,
/* Other ajax params */
});     

or

var pData = { testVar: "Example." };
$.ajax({
/* Other ajax params */
data: pData,
/* Other ajax params */
});     

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.