note 29581 deleted from function.urlencode by tularis
| From: | tularis@php.net | Date: | Sun, 19 Dec 2004 09:42:11 +0000 |
| Subject: | note 29581 deleted from function.urlencode by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-82019@lists.php.net to get a copy of this message | ||
Note Submitter: alexanderangelov at abv dot bg
----
When you're filling in a form sometimes you might forget some of the
required fields and when you go back everything is empty
and you have to fill in all of the fields again. To avoid this you can
pass the values of the filled fields to the same page. But some of the
values of the variables can contain spaces, names for example:'John Smith'.
So i use str_replace() instead of urlencode() to replace spaces with '+'
and 'John Smith' turns into 'John+Smith'. Here is the example:
<?php
function get_rid_of_spaces_and_empty_values($name, $city, $mail, $phone) {
$href_link = "your_form.php";
if ($name) {
$name = str_replace(" ", "+", $name);
$href_link .= "?name=$name";
}
if ($city) {
$city = str_replace(" ", "+", $city);
$href_link .= $href_link == "your_form.php" ? "?city=$city" :
"&city=$city";
}
if ($mail) {
$mail = str_replace(" ", "+", $mail);
$href_link .= $href_link == "your_form.php" ? "?mail=$mail" :
"&mail=$mail";
}
if ($phone) {
$phone = str_replace(" ", "+", $phone);
$href_link .= $href_link == "your_form.php" ? "?phone=$phone" :
"&phone=$phone";
}
return $href_link;
} /*If there is no value for $name and $city == city_value,
$href_link will be "your_form.php?city=city_value";
else it will be "your_form.php?name=name_value&city=city_value" etc.*/
function check_all_inputs_in_the_form($name, $city, $mail, $phone) {
$href_link = get_rid_of_spaces_and_empty_values($name, $city, $mail, $phone);
if ( (!$name)||(!$city)||(!$mail)||(!$phone) ) {
print(" <html><head><title>form</title></head>\n ".
" <body>Please, go <a href=\"$href_link\">back</a> and fill
in all required fields.\n ".
" </body></html>\n " );
exit;
}
}
if (!$hid) {
?>
<html><head><title>form</title></head>
<body><form name="something" method="post"
action="your_form.php">
name:<input type="text" name="name" <?php if ($name) print(
"value=\"$name\"" ); ?>>
e-mail:<input type="text" name="mail" <?php if ($mail) print(
"value=\"$mail\"" ); ?>>
phone:<input type="text" name="phone" <?php if ($phone) print(
"value=\"$phone\"" ); ?>>
city:<input type="text" name="city" <?php if ($city) print(
"value=\"$city\"" ); ?>>
<input type="hidden" name="hid" value="hid">
<input type="submit" value="submit"
name="submit"></form></body></html>
<?php
}
if ($hid == "hid") check_all_inputs_in_the_form($name, $city, $mail, $phone);
?>
I hope this will be helpful :)