简体   繁体   中英

Show PHP variables on form submit alert box

I have a form on which I am having submit button. On submit it pops a alert box that informs about the data to be submitted. I want to show php variables on that for information(alert box) but not getting it. I have the code down here and seeking for some solution. Any help will be appreciated.

 <form class="form-horizontal" method="post" action="" onsubmit="return confirm('You have created a new batch' + <?php echo $rpp_regn_prescribed_startdate_new1;?> +'. Click OK to confirm or you can ignore by clicking Cancel.');">

You're writing a string outside the single quotes. Just remove your '+ and +' :

<form class="form-horizontal" method="post" action="" 
onsubmit="return confirm('You have created a new batch <?php echo $rpp_regn_prescribed_startdate_new1;?>. Click OK to confirm or you can ignore by clicking Cancel.');">

But make sure there is no single in your PHP string.

This simple example works :

<?php
$rpp_regn_prescribed_startdate_new1 = date("r");
?><!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>test</title>
</head>
<body>
<form class="form-horizontal" method="post" action=""
onsubmit="return confirm('You have created a new batch <?php echo $rpp_regn_prescribed_startdate_new1;?>. Click OK to confirm or you can ignore by clicking Cancel.');">
<input type="submit" value="ok">
</form>
</body>
</html>

The problem you have is that the php car is echoed outside the quotes so it's identified by JavaScript as a JavaScript variable.

To understand it, think that when requesting the page, the server will generate the php and send the html to the web browser. The web only receives html, css and JavaScript, it doesn't get any php code, only the results.

So here, your web browser read this JavaScript code :

<form class="form-horizontal" method="post" action="" onsubmit="return confirm('You have created a new batch' + myphpvar +'. Click OK to confirm or you can ignore by clicking Cancel.');">

And it's not correct because it's not corresponding to any JavaScript reference into your code.

So you just need to have your php var printed inside the string and it will be transparent to your code :

 <form class="form-horizontal" method="post" action="" onsubmit="return confirm('You have created a new batch <?php echo $rpp_regn_prescribed_startdate_new1;?>. Click OK to confirm or you can ignore by clicking Cancel.');">

Will display once proceeded :

<form class="form-horizontal" method="post" action="" onsubmit="return confirm('You have created a new batch myphpvar. Click OK to confirm or you can ignore by clicking Cancel.');">
**Updated Code -** 
<?php
    $rpp_regn_prescribed_startdate_new1 = "Test Content";
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

    <form class="form-horizontal" method="post" action="" onsubmit="return validateForm();">
        <button type="submit" name="button">test</button>
    </form>

    <script>
        var phpVarData = '<?php echo $rpp_regn_prescribed_startdate_new1; ?>';
        function validateForm()
        {
             var confirmResp = confirm("You have created a new batch "+phpVarData+".Click OK to confirm or you can ignore by clicking Cancel.");
             if(confirmResp)
             {
                 return true;
             }
             else {
                 return false;
             }
        }
    </script>
    </body>
</html>

$rpp_regn_prescribed_startdate_new1 is returning a value but it is considered as a variable since it is not in the quotes, just add the quotes surrounding the php code. That should work.

<form class="form-horizontal" method="post" action="" onsubmit="return 
confirm('You have created a new batch' + '<?php echo 
$rpp_regn_prescribed_startdate_new1;?>' +'. Click OK to confirm or you can 
ignore by clicking Cancel.');">
echo "<pre>"; print_r($_POST) ;  echo "</pre>";

or

echo "<script>alert('"; print_r($_POST) echo"');</script>";

Your welcome

try this :

There is no need for the concatenation, you can just echo the variable inside you string

Example 1

<form class="form-horizontal" method="post" action="" 
onsubmit="return confirm('You have created a new batch <?php echo $rpp_regn_prescribed_startdate_new1;?> Click OK to confirm or you can ignore by clicking Cancel.');">

Make sure variable should be a part of your string

This will also work

Example 2

<form class="form-horizontal" method="post" action=""
onsubmit="return confirm('You have created a new batch' + '<?php echo $rpp_regn_prescribed_startdate_new1;?>' +'. Click OK to confirm or you can ignore by clicking Cancel.');">

Example 3

<form class="form-horizontal" method="post" action="" onsubmit="return validate();">
  <input type="submit" value="ok">
</form>

<script>
    var phpVarData = '<?php echo $rpp_regn_prescribed_startdate_new1; ?>';
    function validate() {
         confirm("You have created a new batch " + phpVarData + ".Click OK to confirm or you can ignore by clicking Cancel.");
    }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM