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

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:" + "
" + "Exam: " <?php echo  $newassessment ?> );

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


</script>

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.

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:" + "
" + "Exam: " + formInput );

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:" + "
" + "Exam: <?php echo  $newassessment ?>" );

For example, if the value of $newassessment was "foo", your code would end up like this:

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

Which is invalid because the + operator is missing and foo is (presumably) undefined.

I'm assuming that the JavaScript is supposed to run before the page is actually posted. I.e., it's a JavaScript confirmation to the actual POST, and as such the $_POST array wouldn't be populated. 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:" + "
" + "Exam: " + formval );

Is this code inside the script that receives the post request? 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. Use the Web inspector of chrome or safari to double check that the value is being posted and with the right name.

** You also missed a + sign before concatenating the value of the variable in your javascript. Also surround your on the concatenation with quotes.

Your $_POST array will not be populated until AFTER the form is POSTed, and since your javascript confirmation dialog is a precursor to the POST, your template variable is still not set, and therefore equals ''.

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:" + "
" + "Exam: "+session_value);
     if (confirmMsg==true)
     {
        submitform();   
     }
 }
</script>