1

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

5
  • Yes. But you will need to make another request, as PHP is only run once. This is possible via AJAX, jQuery offers a nice interface for this Commented Feb 19, 2015 at 18:44
  • The problem you will face, and I am sure other answers will outline, is that PHP is server side, whilst JavaScript is client-side, meaning that short of using AJAX you will have to reload the page. Good luck. Check out w3schools.com/ajax. And I'm not sure why you got downvoted, +1 to rebalance. Commented Feb 19, 2015 at 18:45
  • Seriously can someoene explain why i got downvoted Commented Feb 19, 2015 at 18:49
  • The real question is, why do you want it as a php variable? It sounds like you are just displaying the value 42 on the client side when the user types in 42 and presses the button. Commented Feb 19, 2015 at 20:25
  • I want to pass it as a php var and then write that variable to a file Commented Feb 20, 2015 at 7:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.