Save input for next input - php

Well, I have this code:
<form name="download" id="download" method="post" enctype="multipart/form-data">
<div>
<label for="beginning_date"><span class="required">*</span> Beginning Date: </label>
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo date("d.m.Y"); ?>" required="required" />
</div>
<input type="submit" value="Next" name="next"/>
</form>
and php..
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else
{
echo "Wrong formation <br />";
}
}
This is a long form, and I want that if someone inputs a date like this "02.03.20322", and the error appears, they don't have to start over to fill the form... the form already appears with the "fills field"...
I've already tried like these:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($date)) { echo $date; } else { echo date("d.m.Y"); } ?>" required="required" />
but... no changes :(

Looking at your code, there simply is no $date so what you should check for is the existence of $_POST['beginning_date'] instead of $date like so:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if (isset($_POST['beginning_date'])) { echo $_POST['beginning_date']; } else { echo date("d.m.Y"); } ?>" required="required" />

When using method post the submitted values of the user go into the $_POST array. The name of the form element is used as key.
So to retrieve the date submitted by the user, you should use:
$_POST['beginning_date']

Just save it in $_SESSION,
at the top of page or anywhere in your action.php:
if (isset($_POST)){
$_SESSION['date'] = $_POST['beginning_date'];
}
and then:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_SESSION['date'])) ? $_SESSION['date'] : date("d.m.Y");?>" required="required" />
Using session is useful in case that in the future you need to change your action page. but if you want to use post instead(for same page posting), just echo it:
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php echo (isset($_POST['beginning_date'])) ? $_POST['beginning_date'] : date("d.m.Y"); ?>" required="required" />

php..
session_start();
if(array_key_exists('next', $_POST))
{
if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{4}$/', $date))
{
//the code continues...
}
else{
echo "Wrong formation <br />";
$_SESSION['date']=$_POST['beginning_date'];
}
}
html..
<input type="text" size="30" id="beginning_date" name="beginning_date" value="<?php if
(isset($_SESSION['date'])) { echo $_SESSION['date'] } else { echo date("d.m.Y"); } ?>"
required="required" />

Related

how to set input field as readonly or entry field

I am having input item if data is available the field should be readonly else
required field.
<input type="text" name="login_password"
id="login_password"
placeholder="Desgination" value="<?php echo
$row["designation"] ?>" Readonly required>
You can try this out, if there is some value in the field it will echo readonly else it will be blank field with required
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo $row["designation"]; ?>" <?php if(trim($row["designation"]) !="") echo "Readonly"; else echo "required"; ?> >
My 2 cents! Without if inside the html code, a bad habit imho
$readonly = isset($row["designation"]) ? 'required' : '';
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo $row["designation"] ?>" <?php echo $readonly; ?> required>
Try This
<?php
if(!empty($row["designation"])){
$condition = "readonly";
else
$condition = "required";
?>
<input type="text" name="login_password" id="login_password" placeholder="Desgination" value="<?php echo
$row["designation"] ?>" <?php echo $condition; ?>>

read data from text box into php var and then echo

I am new to php and just cant figure how to get the data from a a html textbox and then echo the var.
I am making a registration page, I do everything else but this simple part.
I have a preset value="hi" just for testing if the var populates.
Fyi in the future it will be done after i click a register button. just need to get this.
Thanks all
<input name="fName" id="fName" type="text" value="hi" />
and here is the php which i try to read the data into if the echo is test to check if it populates
<?php
$fName = $_POST['fName'];
//$fName = $_GET['fName'];
echo $fName;
?>
<input type="text" placeholder="NAME" name="name" value="<?php if(isset($_POST['name'])){ echo $_POST['name']; } ?>" />
<input type="text" name="phone" placeholder="PHONE" value="<?php if(isset($_POST['phone'])){ echo $_POST['phone']; } ?>" />
This should work for you:
You have to make a form and submit it! After that you can use the variable$_POST['fName']
<?php
if (isset($_POST['fName'])) //if you also want to check if it is empty use !empty($_POST['fName'])
echo $_POST['fName'];
?>
<form action="" method="post">
<input name="fName" id="fName" type="text" value="hi" />
<input type="submit" name="submit" value="submit!" />
</form>

save fields values during form submit

I want that when the user submits the form, and some reason happen some error, I want that the fields that the user already wrote to be saved, so I want to be show the values the user already wrote.
Im doing this with code below, but I dont understand why, when I submit the form the fields stay empty.
Somebody there see something wrong?
<?php
if (isset($_POST['sendForm'])) {
$f['name'] = $_POST['name'];
$f['content'] = $_POST['content'];
$f['date'] = $_POST['date'];
} ?>
<form name="form" action="" method="post">
<label class="line">
<span class="data">Name:</span>
<input type="text" name="name" value="<?php if (isset($f['name'])) echo $f['name']; ?>"/>
</label>
<label class="line">
<span class="data">Content:</span>
<textarea name="content" rows="3" value="<?php if (isset($f['content'])) echo $f['content']; ?>"></textarea>
</label>
<label class="line">
<span class="data">Date:</span>
<input type="text" name="date" value="<?php if (isset($f['date'])) {
echo $f['date'];
} else {
echo date('d/m/Y H:i:s');
} ?>"/>
</label>
<input type="submit" value="Create" name="sendForm" class="btn"/>
</form>
In short you can set by this way,
<input type="text" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>" />
You do not need to use $f['name']. you can get value directly by $_POST['name'] method.

PHP-Array search algorithm

I have had a look at a few answers but none relates to my specific problem. I have an array that takes user input and sorts the array in an ascending order. That's all good, I now want the user to enter a number for which I can locate the array index, if the number is not located, the function returns -1. I cannot put my finger on the error, I'm getting -1 at all times.
Any help is truly appreciated
This is my form that picks up the information.
<form action="welcome.php" method="POST" value="">
Name: <input type="text" name="fname" value="">
Age: <input type="text" name="age" value="">
Integer :<input type="number" name="integer" value="">
<div class="container">
<p> Please choose numbers: </p>
<label>First:</label>
<input type="number" name="name[]" ><br />
<label>2nd</label>
<input type="number" name="name[]"><br />
<label>3rd</label>
<input type="number" name="name[]" ><br />
<label>4th</label>
<input type="number" name="name[]"><br />
<label>5th</label>
<input type="number" name="name[]"><br />
<label>6th</label>
<input type="number" name="name[]"><br />
<label>7th</label>
<input type="number" name="name[]"><br />
<label>8th</label>
<input type="number" name="name[]"><br />
<label>9th</label>
<input type="number" name="name[]"><br />
<label>10th</label>
<input type="number" name="name[]"><br />
<label>Pos Number</label>
<input type="number" name="pos"><br />
<input type="submit">
And the index locator:
Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.
<?php
$tmp = trim($_POST['integer']);
if (!ctype_digit($tmp)) {echo "Input requires an inetger " ;}
else{
echo "The integer is ".$_POST["integer"]."<br>";
}
echo"You have entered the numbers below <br>";
$name=$_POST['name'];
sort($name);
foreach( $name as $v) {
print $v."<br>";
}
echo "You wish to find the position of ".$_POST['pos']."<br />";
function search($name,$K){
$l=0;
$size=sizeof($name);
$u=$size-1;
$m=0;
while ($l<=$u){
$m = ($l+$u)/2;
round($m);
echo "this is m ".$m;
if ($name[$m]<$K) {
$l=$m+1;
}
else if ($name[$m]==$K) {
return $m;
return $m;
} else {
$u=$m-1;
}
}
return -1;
}
$po=$_POST['pos'];
$b=search($name,$po);
echo "The position of the entered number is".$b;
?>

PHP validation resets the form fields

if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one

Categories