无法使用php表单回显

无法使用php表单回显

问题描述:

My PHP form seems to be operating fine. The page comes up and when I click on the submit button, the isset($_POST['submitted']) condition at the start is hit and the code run.

The only issue is that I have a couple of echo lines in the code to produce a JS alert box. Neither seem to be getting called. I'm not sure that the external PHP functions are being called either as I have no way of testing the value returned.

My PHP looks like this

<?php if (isset($_POST['submitted'])) {
$output = checkData(); 
if ($output != "done")
{
    echo '<script type="text/javascript">alert("' . $output . '"); </script>';
}
else 
{
    createMeeting();
    echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>';
    header('Location: index.php');
}   
} else { ?>
<center>
<form method="POST" action="">
...
<input type="submit" name="submitted" value="Create Meeting">
</form>
<?php 
} 
?>

My checkData() function simply checks to see if the other parts of the form data are empty and exit with either "done" (no errors) or a message if one of the form elements is empty.

createMeeting() will create a meeting based on the data and submit it to my server - currently, it takes the same data as checkData() and then returns.

Both functions come back with no errors when I run it through an online PHP code checker.

I've tried your code and it seems to work fine in my case.

<?php if (isset($_POST['submitted'])) {
$output = "done"; //even if this line is not equal to 'done', the code still works fine
if ($output != "done")
{
    echo '<script type="text/javascript">alert("' . $output . '");</script>';
}
else
{
    //    createMeeting();
    echo '<script type="text/javascript">alert("You meeting has been created. All of the recipients should shortly receive an email"); </script>';
    //    header('Location: index.php');
}
} else { ?>
    <center>
    <form method="POST" action="">
    ...
    <input type="submit" name="submitted" value="Create Meeting">
    </form>
<?php
}
?>

So to my knowledge, there're errors with your createMeeting() or checkData() functions. Can you please be more specific with the error messages (if any)?

Check your php.ini. is it set to display or hide errors? It could have encountered an error in checkdata and have died with no warning.

If you want to pop up alert box then redirect it then that's not the proper way to do it it.

Note: The alert box won't execute or display because the page redirect before displaying it. The best way to do it is to create a JavaScript a function instead

<script>
// this function pop up and redirect your page after closing the alert box
function PopupThenRedirect() {
    // pop up the alert box
    alert("You meeting has been created. All of the recipients should shortly receive an email");
    // redirect your page
    location.href = '/index.php';
}
</script>

<?php if (isset($_POST['submitted'])) {
$output = checkData(); 
if ($output != "done")
{
    echo '<script type="text/javascript">alert("' . $output . '"); </script>';
}
else 
{
    createMeeting(); // make sure this php function exist or else it leads to error
    echo '<script type="text/javascript"> PopupThenRedirect(); </script>';
}   
} else { ?>
<center>
<form method="POST" action="">
...
<input type="submit" name="submitted" value="Create Meeting">
</form>
<?php 
} 
?>