Sebastjan
Sebastjan

Reputation: 1147

Getting variable jQuery -> PHP (not working)

I'm having problems with passing variables from jQuery into PHP. I search for the solutions on the internet, and i came to the AJAX. Never used Ajax before, so i guess that the problem is known.

So i have the following code in "index.php"

$("#inviteForm").submit(function(e) {

e.preventDefault();
var emailVal = $("#email").val();

$.ajax({
 type: "POST",
 url: "processAjax.php",
 data: {email: emailVal},
 success: function(data) {
    alert(data);  
}
});
});

In form, i have one input box (for email) and a submit button (the method is POST).

In processAjax.php i have the following code

<?php
    $x = $_POST['email'];
    return $x;
?>

So if i'm correct, if the $.ajax function is OK, the alert box should pop up. But it doesn't. i've also tried alert(x); but it didn't work.

Any idea what i'm doing wrong

Upvotes: 0

Views: 170

Answers (3)

mallix
mallix

Reputation: 1429

Try this for better data manipulation. Use json_encode from the server side and json datatype to your ajax calls. Then to alert server response, just alert the key of the array like alert(data.value):

$.ajax({
 type: "POST",
 url: "processAjax.php",
 data: {email: emailVal},
 dataType: 'json'
 success: function(data) {
    alert(data.value);  
}

processAjax.php

$result['value'] = $_POST['email'];
echo json_encode($result);

Upvotes: 1

Clifford James
Clifford James

Reputation: 161

Try this:

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

Upvotes: 2

Moseleyi
Moseleyi

Reputation: 2859

Try echo $x; instead of return $x;

Upvotes: 7

Related Questions