Jump to content

keeping user input after error and success message


Go to solution Solved by cyberRobot,

Recommended Posts

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

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

 

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.

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 by lukew96
  • Solution

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>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.