Note that there are some explanatory texts on larger screens.

plurals
  1. POecho PHP variable messages to php form
    text
    copied!<p>I have a basic post form in new.php and I am wondering how I can echo (or display) php script variable messages from new1.php on it.</p> <p>new.php:</p> <pre><code> &lt;body&gt; &lt;div id="contact_form"&gt; &lt;form name="contact" method="post" action="new1.php" id="contact"&gt; &lt;fieldset&gt; &lt;label for="firstName" id="firstName_label"&gt;First Name*:&lt;/label&gt; &lt;input type="text" name="firstName" id="firstName" size="36" value="" class="text-input" /&gt; &lt;label class="error" for="firstName" id="firstName_error"&gt;This field is required.&lt;/label&gt; &lt;br /&gt; &lt;label for="lastName" id="lastName_label"&gt;Last Name:&lt;/label&gt; &lt;input type="text" name="lastName" id="lastName" size="36" value="" class="text-input" /&gt; &lt;label class="error" for="lastName" id="lastName_error"&gt;This field is required.&lt;/label&gt; &lt;br /&gt; &lt;label for="email" id="email_label"&gt;Email*:&lt;/label&gt; &lt;input type="text" name="email" id="email" size="36" value="" class="text-input" /&gt; &lt;label class="error" for="email" id="email_error"&gt;This field is required.&lt;/label&gt; &lt;label class="error" for="email" id="email_error2"&gt;This is not a valid email address.&lt;/label&gt; &lt;br /&gt; &lt;label for="postcode" id="postcode_label"&gt;Postcode:&lt;/label&gt; &lt;input type="text" name="postcode" id="postcode" size="12" value="" class="text-input" /&gt; &lt;label class="error" for="postcode" id="postcode_error"&gt;This field is required.&lt;/label&gt; &lt;br /&gt; &lt;input type="submit" name="submit" id="submit_btn" value="Send" /&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p>new1.php - returns the messages in $msg_to_user. As you can see I echo them on this page, but how can I echo them back to new.php where the form is?</p> <pre><code>&lt;?php // set variables to null $firstName = ""; $lastName = ""; $email = ""; $postcode = ""; $msg_to_user = ""; include_once "connect_to_mysql.php"; // set variables from form // be sure to filter this data to deter SQL injection, filter before querying database $firstName = $_POST['firstName']; $lastName = $_POST['lastName']; $email = $_POST['email']; $postcode = $_POST['postcode']; $sql = mysql_query("SELECT * FROM newsletter WHERE email='$email'"); $numRows = mysql_num_rows($sql); // first name is mandatory if (!$firstName) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;Please enter your name.&lt;/font&gt;&lt;/h4&gt;'; // email is mandatory } else if (!$email) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;Please type an email address ' . $firstName . '.&lt;/font&gt;&lt;/h4&gt;'; // check email is valid } else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;' . $email . ' is not a valid email address. &lt;/font&gt;&lt;/h4&gt;'; // check postcode is a number } else if (!is_numeric($postcode)) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;Postcode must be a numeric value.&lt;/font&gt;&lt;/h4&gt;'; // check postcode is greater than 4 chars } else if (strlen ($postcode) &lt; 4) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;Postcode must be at least 4 characters.&lt;/font&gt;&lt;/h4&gt;'; // check postcode is less than 12 chars } else if (strlen ($postcode) &gt; 12) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;Postcode must be less than 12 characters.&lt;/font&gt;&lt;/h4&gt;'; // check email doesn't exist } else if ($numRows &gt; 0) { $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="FF0000"&gt;' . $email . ' is already in the system.&lt;/font&gt;&lt;/h4&gt;'; // if all test passed, insert details into database } else { $sql_insert = mysql_query("INSERT INTO newsletter (firstName, lastName, email, postcode, dateTime) VALUES('$firstName','$lastName','$email','$postcode',now() )") or die(mysql_error()); $msg_to_user = '&lt;br /&gt;&lt;br /&gt;&lt;h4&gt;&lt;font color="0066FF"&gt;Thanks ' . $firstName . $lastName . ' , you have been added successfully.&lt;/font&gt;&lt;/h4&gt;'; $firstName = ""; $lastName = ""; $email = ""; $postcode = ""; } ?&gt; &lt;html&gt; &lt;body&gt; &lt;p&gt;First Name: &lt;?php echo $firstName; ?&gt;&lt;/p&gt; &lt;p&gt;Last Name: &lt;?php echo $lastName; ?&gt;&lt;/p&gt; &lt;p&gt;Email: &lt;?php echo $email; ?&gt;&lt;/p&gt; &lt;p&gt;Postcode: &lt;?php echo $postcode; ?&gt;&lt;/p&gt; &lt;p&gt;Message: &lt;?php echo $msg_to_user; ?&gt;&lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload