Jump to content


Photo

problem with ajax and PHP


  • Please log in to reply
3 replies to this topic

#1 amr

amr

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 21 July 2011 - 07:55 AM

hi guys,
i ahve aproblem to use ajax and php together.
in the following code please tell me how to display the contents of the variable "$num" using echo.....some mates told me that i have to use ajax...if so, please help me

1-in javascript code i have:


<form action="thisBackUpphpProject6Formate.php" method="get">

Family Name:<input type="text" name="field4"><br>
First Name:<input type="text" name="field5"><br>

<input type="Submit">
NUM: <input type="text" id="idnum" name="num" value=""><br>
</form>










2-the value of the field NUM is populated using :

function setrownum(par)
{
document.getElementById('idnum').value = par;
num=par;
}

3-In PHP code, i have :

$field4=$_GET['field4'];
$field5=$_GET['field5'];
$num=$_GET['num'];


4- when i try this:
echo "$num"; /*NOTHING IS DISPLAYED */

please guid and help me...to display the contents of "$num"

#2 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 21 July 2011 - 10:36 PM

In what manner and where are you trying to display the value of that field? If you explain the exact workflow you are trying to achieve, we can better advise you on whether or not ajax is actually necessary.

Edited by rc69, 21 July 2011 - 10:37 PM.


#3 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 21 August 2011 - 12:41 PM

I agree, it doesn't look like any use of AJAX is even being utilized so far.

My concern is where the JavaScript function setrownum() is being invoked. If it is never called to set the value, then of course your value will be absent when trying to access it, as it was submitted as being blank.

#4 JimmyJames

JimmyJames

    Young Padawan

  • Members
  • Pip
  • 39 posts
  • Gender:Male
  • Location:Calgary, AB

Posted 19 December 2011 - 08:07 PM

Check out jQuery, the slight learning curve makes AJAX incredibly easier and much more understandable. http://api.jquery.com/jQuery.ajax/

Example jQuery code to handle your form

$(function(){
  $("#id-of-your-form").submit(function(){
	var formdata = $(this).serialize();  // Creates a JSON array holding your form data
	$.ajax({
	  url: 'formhandler.php',
	  type: 'POST',
	  dataType: 'HTML',
	  data: formdata,	   // Pass the formdata JSON
	  complete:function(result){
		$("#pageContent").html(result.responseText);
	  }
	});
  });
});


Now because we recursively added form items to be sent through using the serialize() function from jQuery, we can make the form as BIG as we want, and not need to add more lines of code to send through each variable.

Your formhandler.php might look like:



  // Output all the POSTed keys and values
  foreach($_POST as $k=>$v){
	echo "$k => $v
";
  }

?>


Hope that made some sense, this code was only for example purposes and has not been run or debugged.

Edited by JimmyJames, 19 December 2011 - 08:11 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users