1

Here is my sample code hope you can help me. how can I pass a JavaScript variable to php variable why it keep popping true even I choose cancel?

JavaScript

var jsChoose = confirm("are you sure?");

PHP

$phpChoose = "<script>document.write(jsChoose);</script>";
if($phpChoose==True){
  echo "true";
}
else{
  echo "false";
}
Cameron
  • 1,049
  • 12
  • 24

4 Answers4

2

Use an ajax function and send variables through that to the php

 $.ajax({
   type: "POST",
   url: "file url with php",
   data: {myVariable:value},
   complete: function(data){
       console.log(data);
   }
 });
Cameron
  • 1,049
  • 12
  • 24
Chris Hutchison
  • 601
  • 6
  • 20
2

One of the best way to pass information from the client side (JS) to the server side (PHP) is to use an html form. The html form can be hidden from the webpage if you do not want users to see it.
You want this form to POST the required information to a PHP script.
For example, you wanted to pass a boolean variable to the server side which is captured from a confirm box. So, let's make an form using the value returned from the confirm box to be POST to the server side.

The html form:

<form id="myForm" action="testBoolean.php" method="post">
        <input type="hidden" id="inputBoolean" value="5"  name="boolean">
</form>

The javascript (put this below your form, you may get errors if you don't)

<script>
var jsChoose = confirm("are you sure?");

document.getElementById("inputBoolean").value = jsChoose;

document.getElementById("myForm").submit();
</script>

And now you need a php page called "testBoolean.php" and in it you retrieve the posted information. For this demonstration it will simply echo out the value of the POSTed boolean variable that was given by your confirm box.

testBoolean.php :

<?php
$boolean= htmlspecialchars($_POST['boolean']);

echo $boolean;

?>

Remember what's inside the $_POST[''] brackets is the name of your input field that you have submitted. Hope this helps, good luck and best regards.

Jamin
  • 1,362
  • 8
  • 22
0

There's also one more way using a query string, as follows:

<?php
if ( isset($_SERVER['QUERY_STRING'])  && $_SERVER['QUERY_STRING'] != null ) {
    $qs = htmlentities($_SERVER['QUERY_STRING']);
    list(,$ans) = explode("=",$qs);
    if ($ans) {
      echo "Your answer is $ans. ";
    }
    exit("Thank you");
}
?>

You could put the PHP code at the top of a web page, followed by HTML which contains the following JavaScript:

<script>
var ans = false;
if( ans = confirm("Are you sure?")) {
    this.location += "?answer=" + ans;
}
else
{
   document.write("Action cancelled since you seem unsure");
}   
</script>

While Ajax is certainly elegant, dynamically generating a query string containing the variable is a viable option as well. If desired, you could POST the query string to another page without hard-coding a form by taking advantage of stream_context_create(), as follows:

<?php
error_reporting(E_ALL);

if ( isset($_SERVER['QUERY_STRING']) && ($_SERVER['QUERY_STRING'] != NULL)) 
{
  $url = 'http://www.example.com/some_page.php';
  $qs = htmlentities($_SERVER['QUERY_STRING']);
  list($key,$value) = explode("=",$qs);
  $arr = array($key => $value);
  $options = array(
    'http'=>array(
      'method'=>"POST",
      'header'=>
        "Accept-language: en\r\n".
        "Content-type: application/x-www-form-urlencoded\r\n",
      'content'=>http_build_query( $arr )
  ));

  $context = stream_context_create($options);

  $fp=fopen($url,'rb',false,$context);
  fpassthru($fp);
  fclose($fp);
  exit;
}
?>

At the desired url, you would just need to add a script that displays the content, such as:

<?php
var_dump($_POST['answer']);
slevy1
  • 3,797
  • 2
  • 27
  • 33
0

Simple way to validate password

we are using custom validity in javascript here i just created a register from with name, email and password

to confirm the password i have used javaScript a simple method.

        var password = document.getElementById("password"), confirm_password = document.getElementById("confirm_password");

        function validatePassword(){
          if(password.value != confirm_password.value) {
            confirm_password.setCustomValidity("Passwords Does not Match");
          } else {
            confirm_password.setCustomValidity('');
          }
        }

        password.onchange = validatePassword;
        confirm_password.onkeyup = validatePassword;
I have added padding of 5px but not required to perform this process just to add some style.
div{
  padding : 5px;
}
Html page with a simple from. No need of PHP

Give the ID as i have given in input field to identify them in JS

<html>
  <form>
    <div style="padding:5px">
      <div>
        <input type="text" name="user_name" placeholder="Name">
      </div>
      <div>
        <input type="email" name="user_email" placeholder="Email">
      </div>
      <div>
        <input type="password" id="password" name="user_pass" placeholder="Password">
      </div>
      <div>
       <input type="password" id="confirm_password" name="user_conf_pass" placeholder="Confirm Password">
      </div>
      <button type="submit" >Register</button>
    </div>
  </form>
</html>
Ahamed Rasheed
  • 751
  • 9
  • 10
  • Welcome to SO. It's not clear to me how this answers the question. See https://stackoverflow.com/help/how-to-answer for help. – Nick May 29 '18 at 21:11