Display the input form - php

I have code in html for a form which needs to be filled. When the button 'OK' is clicked, values are sent to a php script. I use $_POST. Can I display the same form when input is not of the right format but do this only inside my php script?
This is where I check some of my fields but I don't know how to re-display the form.
if (isset($_POST["name"])) {
$name = $_POST["name"];
}
if (isset($_POST["date"])) {
$date = $_POST["date"];
}
Thanks a lot.

You could try something like this:
<?php
foreach($_POST as $key => $value) {
$$key = $value;
}
?>
<form method="post" action="">
<input type="text" name="name" value="<?php echo !empty($name) ? $name : 'Fill in your name'; ?>" />
<input type="text" name="age" value="<?php echo !empty($age) ? $age : 'Fill in your age'; ?>" />
<input type="text" name="what" value="<?php echo !empty($what) ? $what : 'what'; ?>" />
<input type="text" name="ever" value="<?php echo !empty($ever) ? $ever : 'ever'; ?>" />
<input type="submit" value="Go" />
</form>
If you are looking for a php template engine to split the PHP and HTML, I recommand Smarty
EDIT
To split the HTML and PHP without an engine, you could do something like combine the functions file_get_contents() and str_replace like here:
Template.html
<form method="post" action="">
<input type="text" name="name" value="#_name_#" />
<input type="submit" value="Go" />
</form>
PHP
<?php
$template = file_get_contents('template.html');
foreach($_POST as $key => $value) {
$template = str_replace('#_'.$key.'_#', !empty($value) ? $value : '');
}
echo $template;
?>
That way you get the .html file, and replace #_name_# with the post or a default value.
Still I recommand you to use Smarty

Related

PHP form post undefined index

I have an issue about HTML post to PHP script.
But the values are not posted.
My form.php file codes are;
<form action="http://xxxx/valid.php" method="post">
Name: <input name="Name" value='' type="text" />
Sur Name: <input name="SurName" value='' type="text" />
<input id="submit" type="submit" value="Send" />
</form>
and valid.php codes are;
<?php
echo $_POST["Name"];
echo $_POST["SurName"];
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
die();
?>
I get the blank page and I get this error.
Undefined index: Name Undefined index: SurName
I working on PHP 5.6
What is wrong?
Its Solved !
Changed the http://xxxx/valid.php to /valid.php and its worked.
Try This One.
<form action="http://xxxx/valid.php" method="post">
Name: <input type="text" name="Name" value='' />
Sur Name: <input type="text" name="SurName" value=''/>
<input id="submit" type="submit" value="Send" />
</form>
Try this
$Name = isset($_POST['Name']) ? $_POST['Name'] : '';
$SurName = isset($_POST['SurName']) ? $_POST['SurName'] : '';
echo $Name;
echo $SurName;
try this in valid.php file:
<?php
if(isset($_POST["Name"]) && isset($_POST["SurName"])){
echo $_POST["Name"];
echo $_POST["SurName"];
foreach($_POST as $key=>$value)
{
echo "$key=$value";
}
}
die();
?>
I tested your code, all is working fine with me.
if the code is correct then the issue is in the configuration of server or installation (something like that.)
check your configuration maybe it can help...
Well if it's solved then cheers...
happy coding...

Passing PHP variable data onto another page after validation

While I found something similar to this question on here it didn't answer my question outright.
I have set up this php script to validate the form data, which works, after its validated I want it to then pass the info onto another script page to let the user then verify their input data and then mail the data. Its at this state that I'm having trouble. I've spent the last few days trying to find a solution to this and unfortunately coming up short.
<?php
$name_error = '';
$email_error = '';
$comments_error = '';
$error = false;
if (!empty($_POST['submitted']))
{ //if submitted, the validate.
$name = trim($_POST['name']);
if (empty($name))
{
$name_error='Name is required';
$error = true;
}
$email = trim($_POST['email']);
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
$email_error='E-mail address not valid';
$error = true;
}
$comments = trim($_POST['comments']);
if (empty($comments))
{
$comments_error='Comments are required';
$error = true;
}
if ($error == false)
{
$name_send = $name;
$email_send = $email;
$comments_send = $comments;
/* Redirect visitor to the thank you page */
header('Location: /mail.php');
exit();
}
}
The form this is attached to:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
<label>Your Name</label><br />
<input type="text" name="name" style="width:95%" class="text" value='<?php echo htmlentities($name) ?>' />
<br/>
<span class='error'><?php echo $name_error ?></span>
<br />
<label>Email</label><br />
<input type="email" name="email" style="width:95%" class="text" value='<?php echo htmlentities($email) ?>' />
<br/>
<span class='error'><?php echo $email_error ?></span>
<br />
<label for="comments" style="font-size:16px;">Feedback Comments</label><br />
<textarea name="comments" style="width:95%;" rows="8" value='<?php echo htmlentities($comments) ?>'></textarea>
<br />
<span class='error'><?php echo $comments_error ?></span>
<br />
<input type="checkbox" name="allowCommentPublish" checked="checked" />
<label for="allowCommentPublish" style="font-size:10px;">Allow these comments to be used on our website</label>
<fieldset class="optional">
<h2>[ OPTIONAL ]</h2>
<label>Company Name</label><br />
<input type="text" name="companyName" style="width:95%" class="text" />
<br/>
<label>Phone</label><br />
<input type="text" name="phone" style="width:95%" class="text" /><br/>
<div style="margin:5px 0px;">
<input type="checkbox" name="incmarketing" />
<label style="font-size:10px;"> Yes, you can email me specials and promotions.</label>
<br/>
</div>
</fieldset>
<fieldset>
<input type="submit" name="submitted" value="Send" />
</fieldset>
I will point out im focusing on the main data inputs: Name E-mail and comments.
I need the info from this form to be sent onward but i dont know exactly how to do this and any help will be appreciated greatly.
For passing the values to next page you will have to use either of the three methods.
1. Set cookies with the data.
2. Use global variable session.
3.Pass the data in the url.
For cookies u can set cookies with the values like
setcookie('name',$name);
in ur next page read those cookie data
For sessions:
$_SESSION['name']= $name;
for reading data from cookies & session:
$name = $_COOKIE['name'];
$name = $_SESSION['name'];
For using sessions you must add the line
session_start();
at the start of both the pages that send or receive(use) the data
and for urls
header('Location: /mail.php?name=$name&email=$email&comment=$comments');
Read more on using session
If you need to pass values from one script to another you can use $_SESSION variables. To start a session use: (at the top of the php script)
session_start();
$_SESSION['somename'] = $somevariable;
To access or get that same variable you can use this:
session_start();
$some_other_variable = $_SESSION['somename'];
or you can use hidden input fields.
You can use hidden fields and javascript to submit the form. However as this is the same php page as the original form you will need an if statement
echo '<form name="newForm" action="newpage.php" method="POST">';
echo '<input type="hidden" name="name2" value"' . $name . '">;
echo '<input type="hidden" name="email2" value"' . $email . '">;
echo '<input type="hidden" name="comments2" value"' . $comments . '"></form>;
echo '<script> if (document.getElementById("name2").value != ""){window.onload = function(){ window.document.newForm.submit(); }} </script>';

Auto fill city after typing in zip code

I need to auto fill fill the CITY: form when typing in the zip code,
it says "undefiened variable: array on line.." where value="< ? php $array['navn'] ">
can someone help ?
http://i62.tinypic.com/hv5fkl.jpg
<div id="search">
<form name="input" action="" method="get">
POST:<input type="text" name="postcode"><br><br>
City:<input type="text" name="navn" value="<?php $array['navn'] ?>"><br><br>
<input type="submit" value="search">
</form>
</div>
</div>
<?php
if(isset($_GET['postcode'])){
$postcode = $_GET['postcode'];
$content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');
$array = json_decode($content, true);
echo $array['navn'];
echo "<br>";
}
?>
You'd want to always initialize variables. What happens is that you're trying to access a variable that hasn't been initialized yet.
<?php
$city = ''; // initialize containers
$postcode = '';
if(isset($_GET['postcode'])){
$postcode = $_GET['postcode'];
$content = file_get_contents('http://oiorest.dk/danmark/postdistrikter/'. $postcode . '.json');
$array = json_decode($content, true);
// when the request is made, then assign
$city = $array['navn'];
}
?>
<!-- so that when you echo, you'll never worry about undefined indices -->
<div id="search">
<form name="input" action="" method="get">
POST:<input type="text" name="postcode" value="<?php echo $postcode; ?>"><br><br>
City:<input type="text" name="navn" value="<?php echo $city; ?>"><br><br>
<input type="submit" value="search">
</form>
</div>
In this answer, what happens is that, upon first load (no json request yet), the values are empty but they are declared on top.
When you submit the form, then that variable assignment happens and substitutes the values into that container.
Simple Demo

Echo in a form when user did not insert all info needed

This is a part of my registration form. I want to display back the input user inserted if they forgot to enter all the info needed. However, I get this on my textbox in register form where everyone including my user can see it.
Notice:Undefined variable: name in D:\XAMPP\htdocs\registration.php on line 113
I want it to echo back the input that user had inserted and display it again so that user does not have to enter the same input over again. Help ?
$myusername=($_POST['username']);
$name=($_POST['name']);
if(isset($_POST['username'])) {
echo $_POST['username'];
}
if(isset($_POST['name'])) {
echo $_POST['name'];
}
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Assuming you send the user back to the page they were at previously if the form fails to validate, the POST array is emptied. POST will only carry the information to the page that the form is submitting to.
You can use sessions to save the data in an array, indexed by form field name. Then when the user is sent back to the form, if there are any entries in the array, you can iterate over them through to your form fields.
you can controll the variables with if clause; means you can write:
if ( isset($_POST['send']) && isset($myusername) ) {
echo $myusername;
}
else {
echo '<span style="color:red;">Please complete this field</span>';
}
and do the same in html value for textfiels...
$myusername = isset($_POST['username']) ? ($_POST['username']) : '';
$name = isset($_POST['name']) ? ($_POST['name']) : '';
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
Try this, this should remove your error?
if(isset($_POST['username'])) {
$myusername=($_POST['username']);
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
echo $_POST['username'];
}
if(isset($_POST['name'])) {
$name=($_POST['name']);
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
echo $_POST['name'];
}
Here we are checking for POST value first then we using it.
Write the isset function inside the value of each of your .
Exemple :
<input type="text" name="username" size="60" value="
<?php if( isset( $_POST["myusername"] ) )
echo $_POST["myusername"] ?> "/>
you can use this tutorial for validation of input fields in php
http://www.w3schools.com/php/php_form_validation.asp
or you can use this method
<?php
$myusername="";
$name="";
if(isset($_POST['submit'])){
$myusername = $_POST['username'];
$name = $_POST['name'];
}
?>
<form method="POST" action="">
<input type="text" name="username" size="60" value="<?php echo $myusername; ?>"/>
<input type="text" name="name" size="60" value="<?php echo $name; ?>"/>
<input type="submit" name="submit" size="60" value="submit"/>
</form>

Make errors show on top of a form

How do I make error show on top of form so that if $user->success == true, it wont show my form then. Removing that last else would help, but then form shows after success. One way is to redirect that. Maybe tehre
if (isset($_POST["submit"]))
{
if ($_POST["formid"] == $_SESSION["formid"])
{
$_SESSION["formid"] = '';
$User->signin($_POST['username'], $_POST['password']);
}
else
$User->CheckUser();
if ($User->success == true) {
include ('in.php');
}
if ($User->error)
echo "<p>" . $User->error . "</p>";
else
echo 'Don\'t process form';
$_SESSION["formid"] = md5(rand(0,10000000));
} else {
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
Username:
<input id="username" name="username" type="text" /><br />
Password:
<input id="password" name="password" type="password" /><br />
<input type="hidden" name="formid" value="<?php echo $_SESSION["formid"]; ?>" />
<input type="submit" name="submit" />
<br />
Register
</form>
<?php }?>
Perhaps the simplest approach is to just create a variable $show_form to use to determine whether form is to be shown,
$show_form = true;
if(isset($_POST['submit'])) {
// do your form processing here.
// If you decide everything is good and you don't want to show the form,
// just add this line:
$show_form = false;
} // don't use else here
if (true === $show_form) {
?>
<form>...</form>
<?
}
?>
add this code before your form tag
<?php if (isset($User->error) AND $User->error)?>
<p><?php echo $User->error?></p>
<?php?>

Categories