7 Replies - 680 Views - Last Post: 08 April 2013 - 02:49 AM Rate Topic: -----

#1 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Repopulating HTML for using PHP handler

Posted 05 April 2013 - 01:25 PM

Hey fellow coders, I am trying to find a way to have a user enter these three values in a form of this HTML file. Once that is done it would forward to a PHP handler form that would pass the values through a regex validation and then repopulate the form with what the user entered and add a red "*" next to which one did not pass the regex. Now I seem to have the regex working, but am having an issue repopulating the HTML form and adding the red *. Any point to the right direction would be much appreciated.(I do know I could change the HTML to php and combine them, but I am trying to keep them in two seperate files with the form staying HTML and the handler staying PHP.)

This is the HTML file with the form.

<!DOCTYPE html>

<HTML>

<head>

</head>

<body  style = "width:600px;margin-left:auto;margin-right:auto;">

<form action="myhandler.php" method=post>

<table border="1" align="center" width="100%">
	<tr>
		<th align="center" colspan="2">My Form</th>
	</tr>
	
	<tr>
		<td align="right" width="50%">User email:</td>
		<td width="50%"><input type="text" name="user_email" id="user_email" value=""></td>
	</tr>
	
	<tr>
		<td align="right">User Phone number:</td>
		<td><input type="text" name="user_phone" id="user_phone" value=""></td>
	</tr>
	
	<tr>
		<td align="right">User Street address:</td>
		<td><input type="text" name="user_street" id="user_street" value=""></td>
	</tr>
</table>

<input type="submit" name="submit" value="Submit">
</form>

</body>

</HTML>


This is the PHP Handler file.

<?php $myfile = $_SERVER["REMOTE_ADDR"];
			$file = fopen("$myfile","w+");

$email = $_POST['user_email'];
$emailregex = '/\w+\@\w+\.\w{1,3}/';
$emailmatched = preg_match($emailregex, $email);

$phone = $_POST["user_phone"];
$phoneregex = '/^(\(\d{3}\)\d{3}-\d{4})$ || ^(\(\d{3}\)\d{7}) || ^(d{3}-\d{3}-d{3}) || ^(d{10})/';
$phonematched = preg_match($phoneregex, $phone);

$street = $_POST["user_street"];
$streetregex = '/^\d{1,5}\s\w+\s\w+\.$/';
$streetmatched = preg_match($streetregex, $street);

if($emailmatched && $phonematched && $streetmatched){
		echo "Your form has been submitted" . '<br>';
			echo "Your email is: " . $_POST["user_email"] . '<br>';
			echo "Your phone number is: " . $_POST["user_phone"] . '<br>';
			echo "Your street address is: " . $_POST["user_street"] . '<br>';
				fwrite($file, $email."\n");
				fwrite($file, $phone."\n");
				fwrite($file, $street."\n");
				fclose($file);
}else{
		$formpage = file_get_contents('http://natasha.netsal.selu.edu/~scholeva1/A4.html');
			echo $formpage;
     }
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Repopulating HTML for using PHP handler

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Repopulating HTML for using PHP handler

Posted 05 April 2013 - 01:49 PM

Hey.

It tends to be simpler in these situations to put the form in a PHP page, and have that PHP page process the form. Then you can populate the form using PHP code based on whether or not the fields have been submitted already. For example:
<?php
// Provide a default, empty value for the inputs
$input1 = "";

// Check the input to see if it's valid.
if (isset($_POST["input1"])) {
    // Get the value in a HTML safe string.
    $input1 = htmlentities($_POST["input1"], ENT_QUOTES, "UTF-8");
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Self-Submitting Form Example</title>
    <meta charset="utf-8">
</head>
<body>
    <form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
        <input type="text" name="input1" value="<?php echo $input1; ?>">
        <input type="submit">
    </form>
</body>



That can then be expanded to validate the input first, and show errors and/or error messages along side the input boxes if needed.
Was This Post Helpful? 2
  • +
  • -

#3 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Re: Repopulating HTML for using PHP handler

Posted 06 April 2013 - 08:58 PM

Hey Atli,

Thank you for taking your time to respond, I completely agree that a single php page with the form included would be much better to use. Except, for this exercise and what I am looking to learn, I am trying to figure out how to do it as the original problem was stated, with the HTML being the form and the Handler being the PHP file. It is more of an issue as how to repopulate the form values when the handler calls it.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3098
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Repopulating HTML for using PHP handler

Posted 07 April 2013 - 06:11 AM

So separate your code. You're half way there as it is.
I would move this line:
		$formpage = file_get_contents('http://natasha.netsal.selu.edu/~scholeva1/A4.html');

to before the if statement. Then I'd remove the if statement altogether and populate the form with $emailmatched, $phonematched, and $streetmatched.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: Repopulating HTML for using PHP handler

Posted 07 April 2013 - 06:32 AM

He doesn't really need to modify the IF clause though. The form should only be printed if there is a validation error, right? So it's actually in the right place as it is. All that has to be done there is inject the form values back into the HTML and add the red asterisks.

The simplest way would be to use str_replace to modify the inputs. In the HTML form in the original post, each input includes an "id" attribute with a value that matches the name, followed by the empty "value" attribute. So to replace those, you'd do something like:
// The string to find in the HTML, which is the id and value
// attribute of the email field.
$search  = 'id="user_email" value="">';

// The string you want to replace it with, where the value
// attribute has been filled in by the posted value.
$htmlSafeEmail = htmlentities($email, ENT_QUOTES, "UTF-8");
$replace = 'id="user_email" value="' . $htmlSafeEmail . '">';

// And finally check if the email failed validation, and if it
// did, add a red asterisk to the replacement string, after the
// closing of the input tag.
if (!$emailmatched) {
    $replace .= '<span style="color: red;">(*)</span>';
}

// Then do the replace
$formpage = str_replace($search, $replace, $formpage);


Was This Post Helpful? 2
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3098
  • View blog
  • Posts: 10,883
  • Joined: 08-August 08

Re: Repopulating HTML for using PHP handler

Posted 07 April 2013 - 07:27 AM

Right. I'd forgotten about the color coding for errors.
Was This Post Helpful? 0
  • +
  • -

#7 Ghandi_Of_Codez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 26-March 13

Re: Repopulating HTML for using PHP handler

Posted 07 April 2013 - 10:03 PM

Hey and thank you again for the last advice. I did some research on using str_replace, but obviously I am using it incorrectly. These are the following errors I am receiving upon compile. It seems getting the user input to populate the text box was much harder than I first thought.

Notice: Undefined variable: formemail in handler2.php on line 23
Notice: Undefined variable: formphone in handler2.php on line 32
Notice: Undefined variable: replace_street in handler2.php on line 39
Notice: Undefined variable: formstreet in handler2.php on line 41



This is my new code:
<?php $myfile = $_SERVER["REMOTE_ADDR"];
			$file = fopen("$myfile","w+");

$email = $_POST['user_email'];
$emailregex = '/\w+\@\w+\.\w{1,3}/';
$emailmatched = preg_match($emailregex, $email);

$phone = $_POST["user_phone"];
$phoneregex = '/^(\(\d{3}\)\d{3}-\d{4})$ || ^(\(\d{3}\)\d{7}) || ^(d{3}-\d{3}-d{3}) || ^(d{10})/';
$phonematched = preg_match($phoneregex, $phone);

$street = $_POST["user_street"];
$streetregex = '/^\d{1,5}\s\w+\s\w+\.$/';
$streetmatched = preg_match($streetregex, $street);

$search_email  = 'id="user_email" value="">';
$htmlSafeEmail = htmlentities($email, ENT_QUOTES, "UTF-8");
$replace_email = 'id="user_email" value="' . $htmlSafeEmail . '">';
	
if (!$emailmatched) {
	$replace_email .= '<span style="color: red;">*</span>';
}
	$formemail = str_replace($search_email, $replace_email, $formemail);

$search_phone  = 'id="user_phone" value="">';
$htmlSafePhone = htmlentities($phone, ENT_QUOTES, "UTF-8");
$replace_phone = 'id="user_phone" value="' . $htmlSafePhone . '">';
	
if (!$phonematched) {
	$replace_phone .= '<span style="color: red;">*</span>';
}
	$formphone = str_replace($search_phone, $replace_phone, $formphone);

$search_street  = 'id="user_street" value="">';
$htmlSafeStreet = htmlentities($street, ENT_QUOTES, "UTF-8");
$replace = 'id="user_street" value="' . $htmlSafeStreet . '">';
	
if (!$streetmatched) {
	$replace_street .= '<span style="color: red;">*</span>';
}
	$formstreet = str_replace($search_street, $replace_street, $formstreet);

if($emailmatched && $phonematched && $streetmatched){
		echo "Your form has been submitted" . '<br>';
			echo "Your email is: " . $_POST["user_email"] . '<br>';
			echo "Your phone number is: " . $_POST["user_phone"] . '<br>';
			echo "Your street address is: " . $_POST["user_street"] . '<br>';
				fwrite($file, $email."\n");
				fwrite($file, $phone."\n");
				fwrite($file, $street."\n");
				fclose($file);
}else{
		$formpage = file_get_contents('http://example.html');
			echo $formpage;
     }
?>

Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3576
  • View blog
  • Posts: 10,439
  • Joined: 08-June 10

Re: Repopulating HTML for using PHP handler

Posted 08 April 2013 - 02:49 AM

well, you didnít define those variables before you use them, hence the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1