JS prompt into PHP variable

Associate
Joined
4 Feb 2014
Posts
12
I have a JS prompt windows that appears within certain IF statement in PHP and I want the message typed in by the user to be held in a PHP variable so it can be submitted into a database.

The prompt will works so just confused as to how to store it to a PHP variable.

This is the JS code for the prompt box.
Code:
function failComment()
{
var x;

var comment=prompt("Please enter a comment as to why the test failed!","");

if (comment!=null)
  {
  x=comment;
   }
}

Any suggestion would be great, thanks.
 
You will need to submit the value the user enters back to the server, as the javascript is being executed in the client's browser. The simplest way to do this is by using a HTML form.
 
Cue posts about using jquery and xmlhttprequest/ajax. :p

My advice:

Don't use the javascript prompt() function. It's quite ugly and in this case I'm quite certain unnecessary. As planty mentions, use a form akin to:

Code:
<form action="comment-failure-receiving-file.php" method="post">
  <label for="comment">Please enter a comment as to why the test failed.</label>
  <input type="text" id="comment" name="comment"/>
  <button type="submit">Submit comment</button>
</form>
 
should have mentioned that the JS prompt box is on the actual submission page. The user enters data into a table then hits submit, it then checks the data via these IF statements. So if it falls under the fail IF statement then I want the user to enter a comment. So I cant really POST again cause I have POSTed the previous pages form values to this page. If you know what I mean! lol
 
Instead of showing an ugly prompt dialog when those conditions are met, why not dynamically show/hide a textarea element for the user to type the details in to.

Much nicer UI, and better UX for the user as there are no unexpected alerts.

Yea that sounds a better option. How would I show/hide a text area?

Would you just have it hidden on the initial form and if it fails then show the hidden comments box? Which brings my next question, how would I unhide the textarea?

Sorry, but im not strong on this stuff! lol
 
Can you not present the user with a comments form/page?

Or if possible, replicate the checks client-side and show a comment field if the checks fail.

Alternatively, ignore the 'mumsnet' crowd and use asynchronous requests :cool:
 
should have mentioned that the JS prompt box is on the actual submission page. The user enters data into a table then hits submit, it then checks the data via these IF statements. So if it falls under the fail IF statement then I want the user to enter a comment. So I cant really POST again cause I have POSTed the previous pages form values to this page. If you know what I mean! lol

You could store the POSTed data into a session or use hidden form tags. That way the data can be re-used.
 
Back
Top Bottom