0

I'm beginner to php. I wanted to get value when page loading using prompt box in php code.

Here is the code `

<?php function prompt($prompt_msg) {
    echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."'); </script>");

    $answer = "<script type='text/javascript'> document.write(answer); </script>";
    return($answer);
}

//program
$prompt_msg = "Please Enter the Cash price.";
$price = prompt($prompt_msg);
echo $price ; ?>

It worked perfectly.But when i tried this equation ,$name value not active

                `    $balance="";

                    // Calculate balance
                    $p=(int)($price)+0;
                    $balance= $p - $sub_total;
                    echo $balance;

It always give negative value of $sub_tot,Because $float act as always 0,I used

this equation $balance= $price - $sub_total;,When i echo balance its also gave same output

Please give me solution

  • THere is not prompt from PHP. That javascript will not run until it is sent to the browser, thats where javascript runs – RiggsFolly Jul 25 '18 at 13:16
  • So you may need to research AJAX if this is what you want to do. Or create a `
    ` to allow user to enter a value that you then use in PHP
    – RiggsFolly Jul 25 '18 at 13:18
  • PHP fully runs on the server and is sent to the client where the HTML and JavaScript runs. They do not run at same time. Only way to get user information from the user and get it back to PHP is through another HTTP Request (aka form submission, Ajax request, etc) – epascarello Jul 25 '18 at 13:18
  • 3
    *"It worked perfectly"* – That's… surprising. – deceze Jul 25 '18 at 13:23
  • Yes when echo $price it will print the value which gave when page loading using prompt box – Isuru Dhananjaya Ranaweera Jul 25 '18 at 13:25
  • 2
    That's because `document.write(answer);` writes it into the document. Not because `echo $price` outputs it. – deceze Jul 25 '18 at 13:28

0 Answers0