5 Replies - 9022 Views - Last Post: 03 February 2010 - 05:19 AM

#1 kinged007   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 05-November 09

Letting PHP handle POST data through $.ajax()

Posted 02 February 2010 - 06:00 AM

Hi guys (and gals)

Im having the following problem ,please help me.

Im executing the following script
$('#testform').submit(function(){
  $.ajax({
    type: 'post',
    url : 'ajaxrequest.php',
    data : $(this).serialize(),
    success : function(data){
         $('#content').html(data);
    }
  });
  return false;
});




I want to send all the data in the form to the PHP file for it to handle and validate where necessary, then return the results.
But the PHP doesnt pick up the POST requests, nor does the 'data' sent from the jquery have any value.
Can you send the entire form to a php script through ajax and let the php do the handling?
Would it be better to send all the data as a GET request, and would that be secure?

Sort of new to jQuery so trying to understand what it is doing, and what its sending/receiving. My PHP is not bad.

Thanks in advance
Would it be better to send the request

Is This A Good Question/Topic? 0
  • +

Replies To: Letting PHP handle POST data through $.ajax()

#2 teampoop   User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 140
  • Joined: 19-June 08

Re: Letting PHP handle POST data through $.ajax()

Posted 02 February 2010 - 06:54 AM

Good morning,
What you're doing should work, what does your PHP look like?

markie
Was This Post Helpful? 0
  • +
  • -

#3 kinged007   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 05-November 09

Re: Letting PHP handle POST data through $.ajax()

Posted 02 February 2010 - 11:23 AM

Good morning (or evening now... ) :)

My PHP looks like this:

if(isset($_REQUEST['testform'])){
	if($_REQUEST['name'] != '' ){ echo 'Please enter your name'; }
	if($_REQUEST['email'] != '' ){ echo 'Please enter your email'; }
}


its just simplified for the purpose of trying to fix it.

When I dump the variable 'testform' through the $_POST or the $_REQUEST variables, it returns absolutely nothing. The error reporting says that its undefined??
The input names ('name' and 'email' ) also return undefined....

:(

Thanx for your help
Was This Post Helpful? 0
  • +
  • -

#4 Wimpy   User is offline

  • R.I.P. ( Really Intelligent Person, right? )
  • member icon

Reputation: 159
  • View blog
  • Posts: 1,038
  • Joined: 02-May 09

Re: Letting PHP handle POST data through $.ajax()

Posted 03 February 2010 - 02:25 AM

try this:
$('#testform').submit(function(){
  $.ajax({
    type: 'post',
    url : 'ajaxrequest.php',
    data : $(this).serialize(),
    success : function(data){
         $('#content').html(data);
    },
    error : function(xhr, status, error)
    {
        alert(xhr.responseText);
    }
  });
  return false;
});
this will give you an alert box on error and perhaps some information for why it failed! :) Hope it helps! :) also make sure the script is executed at all! :)

View Postkinged007, on 02 February 2010 - 03:00 PM, said:

Hi guys (and gals)

Im having the following problem ,please help me.

Im executing the following script
$('#testform').submit(function(){
  $.ajax({
    type: 'post',
    url : 'ajaxrequest.php',
    data : $(this).serialize(),
    success : function(data){
         $('#content').html(data);
    }
  });
  return false;
});




I want to send all the data in the form to the PHP file for it to handle and validate where necessary, then return the results.
But the PHP doesnt pick up the POST requests, nor does the 'data' sent from the jquery have any value.
Can you send the entire form to a php script through ajax and let the php do the handling?
Would it be better to send all the data as a GET request, and would that be secure?

Sort of new to jQuery so trying to understand what it is doing, and what its sending/receiving. My PHP is not bad.

Thanks in advance
Would it be better to send the request

Was This Post Helpful? 0
  • +
  • -

#5 kinged007   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 05-November 09

Re: Letting PHP handle POST data through $.ajax()

Posted 03 February 2010 - 04:48 AM

Thanx guys, but im afraid nothing is working.

No error is brought back in the jQuery, and the file is being called. If i place an echo statement outside the if statement. It returns the echo statement.
So I know that the file is being called and its returning data.
What is not doing is sending the data to the PHP file in the first place.
WHY ME? lol

Ive tried changing the data feed in the jquery to 'test=helllooo' for testing, and it returns the 'hellloooo' part without a problem.
So from all the testing, It seems you cant send the entire form to the PHP to sort out, only each input item.... that sucks.

:/
Was This Post Helpful? 0
  • +
  • -

#6 kinged007   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 05-November 09

Re: Letting PHP handle POST data through $.ajax()

Posted 03 February 2010 - 05:19 AM

CRACKED IT.... :D

in the data field in the ajax, instead of '$(this).serialize()' use '$(this+':input').serialize()' ... that grabs all the inputs (including textarea) and sends it via post to the PHP file. That leaves the php to sort through it.
Also, in the url bit of the ajax request, add '?sendform=true' to the end of the filename to allow conditional statements to the PHP.

Thanx for your help guys, but reading the whole jQUery documentation seemed to be the only way out. :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1