lukew96 Posted December 4, 2013 Share Posted December 4, 2013 hi i was just wondering how i would go about keeping user input in a field if an error occurs e.g. if i accidentally type in a special character into my form it will display a message saying : is not aloud or something like that but once this is shown all user input is lost so they would have to type it in again, ive tryed going online and i cant seem to find anything that works as it comes up with undifiened index <input type="text" name="clientname" value="<?php echo htmlspecialchars($_post['clientname']);?> > aswell as this im really in need of a message that says success, ive got no clue how i would go about doing it. what i need is a message that says 'form submitted'once you press submit and when no errors occur. greatly apreciate anyone that can help Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/ Share on other sites More sharing options...
lukew96 Posted December 4, 2013 Author Share Posted December 4, 2013 ( ! ) Notice: Undefined index: address1 in C:\wamp\www\AddLeads\addeadstemplate.php on line 268 Call Stack # Time Memory Function Location 1 0.0000 186656 {main}( ) ..\addeadstemplate.php:0 heres the error Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461215 Share on other sites More sharing options...
cyberRobot Posted December 4, 2013 Share Posted December 4, 2013 (edited) Variables are case sensitive, try <input type="text" name="clientname" value="<?php echo htmlspecialchars($_POST['clientname']); ?>> Note that I used $_POST <-- upper case Edited December 4, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461218 Share on other sites More sharing options...
lukew96 Posted December 4, 2013 Author Share Posted December 4, 2013 changed that but now getting this ( ! ) Notice: Undefined index: clientname in C:\wamp\www\AddLeads\addeadstemplate.php on line 267 Call Stack # Time Memory Function Location 1 0.0000 186584 {main}( ) ..\addeadstemplate.php:0 Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461220 Share on other sites More sharing options...
cyberRobot Posted December 4, 2013 Share Posted December 4, 2013 Have the POST variables been set? For example, are you using the POST variables directly after a form has been submitted? You'll also want to make sure you're using the correct case and name for the index values. In other words, "clientname" in $_POST['clientname'] needs to match the exact name you gave the corresponding form field. clientname != ClientName clientname != client If you need further assistance, it may help to show more code. Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461223 Share on other sites More sharing options...
lukew96 Posted December 4, 2013 Author Share Posted December 4, 2013 (edited) im pretty sure ive set them correctly but i done no heres my whole code <!DOCTYPE HTML> <html> <head> <style> .error {color: #FF0000;} </style> </head> <body> <?php $con = mysqli_connect("localhost","root","","nib"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // define variables and set to empty values $companyname = $firstname = $clientname = $address2 = $county = $city = $postcode = $email = $website = $clubphone = $homephone = $mobilephone = $typesofbusiness = $renewaldate = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $errors = array(); if (empty($_POST["companyname"])) { $errors['companyname'] = "Please Enter Your companyname"; }elseif(preg_match_all("/[(?\/<>*:.@)]/i",$_POST['companyname'],$matches)){ // list of invalid characters #echo '<pre>',print_r($matches),'</pre>'; $invalid_characters = implode($matches[0]); $errors['companyname'] = 'Cannot use '.$invalid_characters; $answer = (isset($_POST['answer'])) ? $_POST['answer'] : ''; } elseif(strlen($_POST['companyname']) > 220) { $errors['companyname'] = 'Company name must be less then 220 characters'; } if (empty($_POST["typesofbusiness"])) { $errors['typesofbusiness'] = "Please Enter Your type of business"; } elseif(strpos($_POST['typesofbusiness'], '/') !== false) { $errors['typesofbusiness'] = 'Cannot use /'; } else { $typesofbusiness = test_input($_POST["typesofbusiness"]); } if (empty($_POST["firstname"])){ $errors['firstname'] = "Please Enter Your First Name"; }elseif(preg_match_all("/[(?\/<>*:.@)]/i",$_POST['firstname'],$matches)){ // list of invalid characters #echo '<pre>',print_r($matches),'</pre>'; $invalid_characters = implode($matches[0]); $errors['firstname'] = 'Cannot use '.$invalid_characters; } if (empty($_POST["clientname"])){ $errors['clientname'] = "Please Enter Your clientname"; }elseif(preg_match_all("/[(?\/<>*:.@)]/i",$_POST['clientname'],$matches)){ // list of invalid characters #echo '<pre>',print_r($matches),'</pre>'; $invalid_characters = implode($matches[0]); $errors['clientname'] = 'Cannot use '.$invalid_characters; } ?> <h2>Add Leads</h2> <p><span class="error">* required field.</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> <table border = "1" bordercolor = ""> <tr> <td>Company Name:</td><td> <input type="text" name="companyname"></td> <td><span class="error">* <?php if (isset($errors['companyname'])) echo $errors['companyname']; ?></span></td> </tr> <tr> <td>Type of Business:</td> <td> <select name="typesofbusiness"> <option value=''> - select type of business -</option> <?php $sql = "SELECT tbl_typesofbusiness.id, tbl_typesofbusiness.Agent FROM tbl_typesofbusiness;"; $res = mysqli_query($con, $sql) or die (mysqli_error($con)); while (list($id, $tob) = mysqli_fetch_row($res)){ echo "<option value='$id'>$tob</option>\n"; } ?> </select> </td> <td><span class="error">* <?php if (isset($errors['typesofbusiness'])) echo $errors['typesofbusiness']; ?></span></td> </tr> <tr> <td>Client name:</td> <input type="text" name="clientname" value="" <?php echo htmlspecialchars($_POST['clientname']); ?>> <td><span class="error">* <?php if (isset($errors['clientname'])) echo $errors['clientname']; ?></span></td> </tr> <tr> <tr> <td><input type="submit" name="submit" value="Submit"></td> </tr> <?php if($_SERVER['REQUEST_METHOD'] == "POST" && count($errors) == 0) { // Do it { $con = mysqli_connect("localhost","root","","nib"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="INSERT INTO tbl_club_contacts (CompanyName, FirstName, clientname) VALUES ('$_POST[companyname]','$_POST[firstname]','$_POST[clientname]; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } mysqli_close($con); } // end if $errors == 0 } ?> </form> </body> </html> Edited December 4, 2013 by lukew96 Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461224 Share on other sites More sharing options...
lukew96 Posted December 4, 2013 Author Share Posted December 4, 2013 i had to get rid of some stuff still the same problem though Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461225 Share on other sites More sharing options...
Solution cyberRobot Posted December 4, 2013 Solution Share Posted December 4, 2013 Based on the form code posted, the "clientname" variable needs to be moved inside the value attribute. To avoid the undefined index error, you can use an isset() test. Instead of this <tr> <td>Client name:</td> <input type="text" name="clientname" value="" <?php echo htmlspecialchars($_POST['clientname']); ?>> <td><span class="error">* <?php if (isset($errors['clientname'])) echo $errors['clientname']; ?></span></td> </tr> Try <tr> <td>Client name:</td> <input type="text" name="clientname" value="<?php if(isset($_POST['clientname'])) { echo htmlspecialchars($_POST['clientname']); } ?>"> <td><span class="error">* <?php if (isset($errors['clientname'])) echo $errors['clientname']; ?></span></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461238 Share on other sites More sharing options...
lukew96 Posted December 4, 2013 Author Share Posted December 4, 2013 cheers cheers cheers cheers cheers cheers cheers cheers cyber robot Quote Link to comment https://forums.phpfreaks.com/topic/284515-keeping-user-input-after-error-and-success-message/#findComment-1461241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.