简体   繁体   English

如何将$ _POST值放入javascript确认框中

[英]how to get $_POST value into a javascript confirmation box

I want to be able to include the name of the exam in the confirmation box, at the moment it does not display the name of the exam. 我希望能够在确认框中包含考试名称,此时它不会显示考试名称。 Does anyone know why? 有谁知道为什么? The name of the exam comes from a text input which should be posted. 考试的名称来自应该发布的文本输入。

Below is the form which contains the text input for the exam name: 以下是包含考试名称的文本输入的表单:

<?php

$newassessment = (isset($_POST['Assessmentnew'])) ? $_POST['Assessmentnew'] : '';

$editsession = "<form action=".htmlentities($_SERVER['PHP_SELF'])." method='post' id='updateForm'>

    <table>
    <tr>
    <th>Assessment:</th>
    <td><input type='text' id='newAssessment' name='Assessmentnew' readonly='readonly' value='' /> </td>
    </tr>
    </table>

    <p><input id='updateSubmit' type='submit' value='Update Date/Start Time' name='updateSubmit' onClick='myClickHandler(); return false;'/></p>

    </form>
";

echo $editsession;


}

?>

Below is the confirmation box: 以下是确认框:

<script type="text/javascript">

        function showConfirm(){

         var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " <?php echo  $newassessment ?> );

         if (confirmMsg==true)
         {
         submitform();   
     }
}


</script>

You need to output the string inside the quotes, otherwise it is taken as a variable: 您需要在引号内输出字符串,否则将其视为变量:

var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: <?php echo  $newassessment ?>" );

For example, if the value of $newassessment was "foo", your code would end up like this: 例如,如果$newassessment的值为“foo”,则代码最终会如下所示:

var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " foo );

Which is invalid because the + operator is missing and foo is (presumably) undefined. 哪个是无效的,因为+运算符丢失了,而foo (大概)是未定义的。

I'm assuming that the JavaScript is supposed to run before the page is actually posted. 我假设JavaScript应该在页面实际发布之前运行。 Ie, it's a JavaScript confirmation to the actual POST , and as such the $_POST array wouldn't be populated. 即,它是对实际POST的JavaScript确认,因此不会填充$_POST数组。 Is that correct? 那是对的吗? If so, you'll need to do the following: 如果是这样,您需要执行以下操作:

var formval = document.getElementById('newAssessment').value;
var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + formval );

Is this code inside the script that receives the post request? 此代码是否在接收post请求的脚本中? If , so then what should be happening is that your condition that checks if the values in $_POST is set is evaluating to false because your value was not posted and assigning '' to the variable $newassessment. 如果,那么应该发生的是,检查$ _POST中的值是否已设置的条件是评估为false,因为您的值未发布并将''分配给变量$ newassessment。 Use the Web inspector of chrome or safari to double check that the value is being posted and with the right name. 使用chrome或safari的Web检查器仔细检查是否正在发布值并使用正确的名称。

** You also missed a + sign before concatenating the value of the variable in your javascript. **在连接javascript中的变量值之前,您还错过了一个+号。 Also surround your on the concatenation with quotes. 用引号括起你的串联。

在POST表单之后,您的$ _POST数组将不会被填充,并且由于您的javascript确认对话框是POST的前身,因此您的模板变量仍未设置,因此等于''。

PHP is ran before the page is loaded in the browser which means your javascript code will not contain a variable if you look in the source. PHP在浏览器中加载页面之前运行,这意味着如果查看源代码,您的javascript代码将不包含变量。

try including this before your confirm statement: 在确认声明之前尝试包括这个:

var formInput = document.getElementById('newAssessment').value;

and make your other line 并使你的另一条线

var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: " + formInput );

Try this, I hope it'll help you 试试这个,我希望它能帮到你

 //hold your $_POST['Assessmentnew'] value into a hidden field like this,

<input type="hidden" name='hid1' id="hid1" value="<?php echo $_POST['Assessmentnew'];?>">

function showConfirm()
{
      var session_value=document.getElementById('hid1').value;
      var confirmMsg=confirm("Are you sure you want to update the following:" + "\n" + "Exam: "+session_value);
     if (confirmMsg==true)
     {
        submitform();   
     }
 }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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