TheCoffee TheCoffee - 3 months ago 9
Ajax Question

Cant Send Value To PHP String With AJAX

What is the actual deal in the Data part ?

<form id="form" name="form">
<input type="text" name="textname" id="textname">
<input type="button" name="submit" id="submit" onclick="post();">
</form>

function post()
{
var textname = $('#textname').val();

$.ajax
({
url: 'post.php',
data: {testurl:textname},
type: "POST",
success: function (data)
{
$('#test').html(data);
console.log(data);
}
});
}


And the post.php

<?php
echo "test";

$testurl = $_POST['textname'];

echo var_dump($testurl);
?>



Echo Test is working but Output for the var_dumb is NULL string(0) - Empty String

Answer

Try to understand one simple thing, in ajax call the data parameter contains the

key : value

pair in it.

Means if your data part is some thing like:

data: {
   var1 : val1,
   var2 : val2,
}

than in your php function you can get these values using its keys i.e. var1, var2.