Jump to content

How to get value from javascript to php


kikilis8

Recommended Posts

I use prompt function which result is the variable "number" entered by user.

function prompt_msg($msg){echo "<script language=\"javascript\">var number=prompt(\"".$msg."\"); </script>";}

How to get ant to use the value of variable "number" to php?

<?php$number = prompt_msg($local->getLocalString('enter_number'));if ($number>0)  {........} ?>

The value of $number now is 0... it does not get. How to get the number value from javascript to php? If someone could write more detail, I would appreciate it!

Link to comment
Share on other sites

Javascript and PHP are not related. If you want to transfer any data from Javascript to PHP you need to use something like an AJAX request. You can't have PHP start to generate a page, then run a Javascript function to get data, then send that data to PHP on the same page. By the time Javascript runs PHP is already finished, they don't run at the same time or in the same place (PHP is on the server, Javascript is on the client).There isn't really a way to have PHP prompt the user for information during runtime. You would need to generate a page with links that include the information, or use a form.

Link to comment
Share on other sites

  • 2 weeks later...

This is just one way to write the javascript end. Obviously you'll have to embed this in a complete html document and have a php script waiting.

	   <script type="text/javascript">		  var hObj; //THE AJAX OBJECT MUST BE DECLARED GLOBALLY		  function hPostData(myData) { //THIS FUNCTION SENDS THE DATA			 hObj = null;			 var myURL = "ajax.php"; //CHANGE THIS TO YOUR PHP SCRIPT URL			 if (hObj = getHObject()){				hObj.onreadystatechange = hStateChanged; //POINTS TO THE FUNCTION BELOW				hObj.open ('POST', myURL, true);				hObj.send (myData);			 }else {alert ("Cannot get an HTTP object");}		  }		  function hStateChanged() { //THIS ONE RECEIVES THE RESPONSE			if (hObj.readyState == 4) { //4 MEANS A RESPONSE WAS RECEIVED FROM YOUR SERVER			   if (hObj.status == 200) { //200 MEANS IT WAS NOT AN ERROR MESSAGE				  printResponse(hObj.responseText);			   }else{				  alert ('Sorry: ' + hObj.status);			   }			}		  }		  function getHObject() { //THIS CREATES AN HTTP OBJECT (DEPENDING ON THE BROWSER)			 var hObj = null;			 try {hObj = new XMLHttpRequest();}			 catch (e) {				try {hObj = new ActiveXObject('Msxml2.XMLHTTP');}				catch (e) {hObj = new ActiveXObject('Microsoft.XMLHTTP');}			 }			 return hObj;		  }		  function printResponse(myText) { //THIS IS JUST A RETURN FUNCTION; ALERT IS A BASIC TEST			 alert('myText');		  }	   </script>

Link to comment
Share on other sites

The variable which I want to use in php code is $number.

function printResponse(myText) { //THIS IS JUST A RETURN FUNCTION; ALERT IS A BASIC TEST			 alert('myText');		  }

instead alert('myText'); I should write my funcion var number=prompt('Insert the number'); Is it correct?

function hPostData(myData) { //THIS FUNCTION SENDS THE DATA

What should I enter in myData?What functions do I need to use to get number value to php?

<?php$number = ??????????? if ($number>0) { .... code ... }?>

Link to comment
Share on other sites

instead alert('myText'); I should write my funcion var number=prompt('Insert the number'); Is it correct?
No. The printResponse function is what runs when the Javascript gets the response from the PHP page. AJAX is just the same as when you use your web browser. When you type in a URL your browser makes a request to the server for the web page, and the server sends a response back to the browser (the actual web page). So you are using Javascript to send a request to the PHP page, and the printResponse function prints the response that the server sends back. You can use the response text to update your page when you get the "answer" from PHP. If you don't want to update the page then you can just remove that. This is the section when it sends the request where it tells it what to do when the response comes back:
			   if (hObj.status == 200) { //200 MEANS IT WAS NOT AN ERROR MESSAGE				  printResponse(hObj.responseText);			   }else{				  alert ('Sorry: ' + hObj.status);			   }

If the response status was 200, meaning OK, then it will print the text. If the response was not 200 (like 404 or something else) then it prints the status code.The hPostData function sends data called myText, so this is how you send a variable called number:

hPostData("number=" + number);

or:

hPostData("number=" + prompt("Enter a number"));

if you don't want to check what they enter before you send it. In the PHP script you would find that in $_POST['number'] (oops, had $_GET before).

Link to comment
Share on other sites

function printResponse(myText) { //THIS IS JUST A RETURN FUNCTION; ALERT IS A BASIC TEST	alert('myText'); }

Whatever you put in the function above will be your client's FINAL response to the entire operation. First you send the data to your server. Then the server sends something back. The function above does something with That data, the stuff coming back from the server.
instead alert('myText'); I should write my funcion var number=prompt('Insert the number'); Is it correct?
So, no, not correct.
function hPostData(myData) { //THIS FUNCTION SENDS THE DATA

What should I enter in myData?

This is the send function, so it assumes you have already gathered the data. Something like what you wrote would go before calling this function. See below:
var number=prompt('Insert the number');   var myData = "myNumber=" + number;   hPostData(myData);

What functions do I need to use to get number value to php?
   <?php      if  ( ($_SERVER['REQUEST_METHOD'] == 'POST')  and  (isset($_POST['myNumber']) ){		  $number = $_POST['myNumber'];	  //other statements   }else{	  // exit routine   }   // return routine, housekeeping      ?>

Link to comment
Share on other sites

My code:

<script type="text/javascript">var hObj; //THE AJAX OBJECT MUST BE DECLARED GLOBALLYvar number=prompt('Insert the number');var myData = "myNumber=" + number;		function hPostData(myData)  { //THIS FUNCTION SENDS THE DATA			 hObj = null;			 var myURL = "lt_zoleliu_misiniai.php"; //CHANGE THIS TO YOUR PHP SCRIPT URL			 if (hObj = getHObject()){				hObj.onreadystatechange = hStateChanged; //POINTS TO THE FUNCTION BELOW				hObj.open ('POST', myURL, true);				hObj.send (myData);			 }else {alert ("Cannot get an HTTP object");}		  }//..........//the rest of code I have not change

In PHP:

	<?if  ( ($_SERVER['REQUEST_METHOD'] == 'POST')  and  (isset($_POST['myNumber']) ))		{ 		 $number = $_POST['myNumber'];				}		else confirm_msg("Doesn't work!!!");?>

How it works for me:In prompt message I enter the number. Fine. But in if ( ($_SERVER['REQUEST_METHOD'] == 'POST') and (isset($_POST['myNumber']) )) it goes to else... And i Get the message: "Doesn't work!!!"An other think is that I want to show me promt message not when I open the window, but after i press the button and it submits the form.

if (isset($_POST["Submit_krepselis"])) {   //HERE i want to get promt() message}

I have try both, exactly after opening the window and in submit... in any case I don't get the $number.

Link to comment
Share on other sites

Does the PHP script actually run? I don't see a line in your javascript where you call the AJAX function:

var number=prompt('Insert the number');var myData = "myNumber=" + number;hPostData(myData); //IS THERE A LINE LIKE THIS IN THE JAVASCRIPT?

I'm sorry if I confused you.

Link to comment
Share on other sites

Does the PHP script actually run? I don't see a line in your javascript where you call the AJAX function:
var number=prompt('Insert the number');var myData = "myNumber=" + number;hPostData(myData); //IS THERE A LINE LIKE THIS IN THE JAVASCRIPT?

I'm sorry if I confused you.

I am really very confused... But it's not your fault :) I have the ability to confuse myself :)I corrected the code as you wrote. Something works :mellow: I get alert message "myText"
<?if  ( ($_SERVER['REQUEST_METHOD'] == 'POST')  and  (isset($_POST['myNumber']) ))		{		 $number = $_POST['myNumber'];				}		else confirm_msg("Doesn't work!!!");?>

and then the message: "Doesn't work!!!"....It doesn't get to php...

You might be trying to use Javascript and PHP the wrong way. What exactly are you trying to do? Would it be easier to just submit a regular form?
I want that user would enter the value after he press the button.
Link to comment
Share on other sites

I want that user would enter the value after he press the button.
What if he just enters the value in a field on the page and then presses the button?<form action="lt_zoleliu_misiniai.php" method="post"><input type="text" name="number"><input type="submit" value="Submit"></form>
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...