1

how to pass java script prompt msg variable value in to php.

<?php 
  echo '<script type="text/javascript"> 
        var msg = window.prompt("Input a value", ""); alert(msg);
        </script>';    
   //then need to use input value to php    
   echo $value; //how to get JS value to here?
 ?>

or is there any other way to get php prompt massages to input value?

1
  • You need to use AJAX for pass data from javascript to PHP Commented Oct 28, 2014 at 6:39

3 Answers 3

3

No, you can't do it directly like this. You need to pass the value from javascript to PHP. One easy way of doing so would be using redirect:

<?php 
  echo '<script type="text/javascript"> 
        var value = prompt("Input a value", "");
        window.location.href = '?value='+encodeURIComponent(value);
        </script>';    
   //then need to use input value to php    
  if (isset($_GET['value'])) {
    echo htmlspecialchars($value);
  }
 ?>

Even though this is very simple example based upon your code, there are more advanced ways of achieving your goal. But this is to get you going.

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

Comments

1

You cannot do that as php is a server side language, it is interpreted before any javascript.

What you need is using AJAX, or using a form.

Both can do the work, the difference is that with Ajax you won't have to reload the page (nor load any other).

Comments

-1

You can try this

echo "<script>var email=prompt('Enter user mail')</script>";

$email="<script>document.write(email)</script>";

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.