user4494285
user4494285

Reputation:

Get data from HTML and save it as a PHP variable

I have started learning php and I have a question.Let's say I have the following html code:

<p id='tobeChanged'>I wil be changed throughout the execution<p>

This paragraph is not static.Its content can be changed from the user with a button which will produce a random number and will replace the paragraphs html. E.g. from

p id='tobeChanged'>I wil be changed throughout the execution<p>

to

<p id='tobeChanged'>42<p><!--changed with a button-->

Now my question.Is it possible to pass the new produced value to a php variable?If possible i would like a long explanation. Also i would like not to use forms(if possible). Thanks In advance

Upvotes: 1

Views: 91

Answers (1)

Manish Sonwal
Manish Sonwal

Reputation: 51

You need to fire an AJAX request on that button click, that will send that value to server making php to read it.

You can do something like this (you need to include jQuery on page):

$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})

At PHP end, you will get the value in

$_POST['randNum']

Maybe that will help.

Upvotes: 1

Related Questions