how to get value from js alert message to store in PHP varible?


#1

I am trying to learn php and js. Here I am trying to put a value in php varible from javascript alert message input box but failed.
here what I have done so far,

// <?PHP and other variable is here....
$number='number'; 

<script>
			function number{
				
				if (fail == "") {
					number = prompt('Please enter Phone Number')
					if (number == "" || number == null) { 
						alert(' You have not specified the Phone Number. Please try again.'); 
						
						return false;
					}
					else {
						
						document.getElementById("number").value;
						
						return true;
					}
				}
				else { alert(fail); return false; }
			}
		</script>

So, actually how I can store the value from js alert to php varible? help me. Thanks for your valuable time and opinion.


#2

PHP runs on the server side, once it’s done it sends the completed document to the users browser.

Then JS runs in the users browser.

JS can not simply store/update variables in PHP as the PHP is no longer running. You also have no such connection between the client side and the server side.

If this is for a form then simply update the form value with JS, when you submit the form PHP should get all the form data.

If this is only to enter a phone number then you might consider looking into doing an AJAX request from JS to PHP after the user inserts a phone number. You will then have to create a PHP file that handles this request spesifically and stores the phone number.