Hi there, I have a question and hope to get answer. After I submitted application form, an error occurred, then I go back and nothing displayed instead of displaying what wasn't error. Here is my code: if (isset($_POST['submit'])) { $title = $_POST['title']; if ($title == "") {$error[] = "TITLE Required!"; }if (!isset($error)) { $sql = "INSERT INTO news VALUES (''$title'')"; $result = mysql_query($sql); mysql_query("SET NAMES 'utf8'"); header("Location: ./application_form.php?success="); }else { foreach ($error as $item) { $output.= "<p>" . $item . "</p>"; } } } else { if (isset($_GET['success'])) $output.= "<p>Successfully Added!</p>"; $output.= "<form enctype=\"multipart/form-data\" id=\"up\" name=\"up\" action=\"\" method=\"post\">\n"; $output.= "<p><tr><td>Title < 100</td><br /><td><input class=\"text\" id=\"title\" name=\"title\" type=\"text\" value='$title' /></td></tr></p>\n"; $output.= "<p><input id=\"submit\" name=\"submit\" type=\"submit\" value=\"Submit\" /></p>\n"; $output.= "</form>\n";} print $output; PHP: Thanks for all.
This should do it... Organise your code better... <?php $error = 0; $error_msg = ''; $output = ''; if(isset($_REQUEST['submit'])) { if(!isset($_POST['title']) || (isset($_POST['title']) && strlen($_POST['title']) == 0)) { $error++; $error_msg .= "The Title field is required.<br />"; } if ($error == 0) { mysql_query("SET NAMES 'utf8'"); $result = mysql_query("INSERT INTO news VALUES (''$title'')"); } } if(isset($_REQUEST['submit']) && $error == 0) $output .= ' <p>Successfully Added!</p>'; else if(isset($_REQUEST['submit']) && $error > 0) $output .= ' <div>'.$error_msg.'</div>'; $output.= ' <form enctype="multipart/form-data" id="up" name="up" action="" method="post"> <p> <table> <tr> <td>Title < 100</td> <td> <input class="text" id="title" name="title" type="text" value='.((isset($_REQUEST['submit']) && $error > 0 && isset($_POST['title']) && strlen($_POST['title']) > 0) ? $_POST['title'] : '').' /> </td> </tr> </table> </p> <p><input id="submit" name="submit" type="submit" value="Submit" /></p> </form>'; echo $output; ?> PHP:
it's great work, but I realized if I put these codes into my system it disappears when I go back...What is wrong with my system????
<?php $error_msg = array(); if(isset($_REQUEST['submit'])) { if(!isset($_POST['title']) || strlen($_POST['title']) == 0) { $error_msg[] = "The Title field is required."; } if (count($error_msg) <= 0) { mysql_query("SET NAMES 'utf8'"); $result = mysql_query("INSERT INTO news VALUES ('" . mysql_real_escape_string($title) . "')"); } } else { $_POST['title'] = ''; } if (isset($_REQUEST['submit']) && count($error_msg) == 0) { $output = ' <p>Successfully Added!</p>'; } else if (count($error) > 0) { $output = ' <div>'.implode('<br />', $error_msg).'</div>'; } $output.= ' <form enctype="multipart/form-data" id="up" name="up" action="" method="post"> <p> <table> <tr> <td>Title < 100</td> <td> <input class="text" id="title" name="title" type="text" value="' . $_POST['title'] . '" /> </td> </tr> </table> </p> <p><input id="submit" name="submit" type="submit" value="Submit" /></p> </form>'; echo $output; ?> PHP: