text box value disappears/empties on PHP validation - php

Hi I have a structure as follows. The issue is once the error message is displayed, the text box content disappears and need to re-type a value again. I need to retain the original text box value/content as it is whilst the message is displayed. How do you think I should change? Thanks.
<?php
if(isset($_POST['subN'])) {
$InNo = $_POST['invc_no'];
$MSG = '';
if($InNo == "")
{$MSG = 'Invoice number is required';}
else
{//some code to save data to a database}
?>
<!DOCTYPE html>
<head><title></title></head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="msgBox"><?php if(!empty($MSG)) echo $MSG; ?></div>
<table>
<tr>
<td><input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php htmlentities('InNo')?>"></td>
<td><input type="submit" name="subN" id="subN" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>

Change
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php htmlentities('InNo')?>">
To
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php echo htmlentities($_POST['invc_no'])?>">
Second Solution:
Put as below
$InNo = ''; // if you doesn't put any value into text box, then you will get undefined error while you use this variable later on so give initial value to null
if(isset($_POST['subN'])) {
continue ....
}
else{
continue ..
}
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php echo htmlentities($InNo); ?>">

Use $_POST['inNo'] in your input box:
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?= $_POST['inNo'] ?>">
Your are not echoing the value, by the way.

You have missed to put echo there.
Change this,
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php htmlentities('InNo')?>">
To
<input type="text" id="invc_no" name="invc_no" size="15" class="colr" value="<?php echo htmlentities('InNo')?>">

Related

Incorrect parameter count in the call to native function 'SHA1'

Trying to get an edit page to work and im getting this error Incorrect parameter count in the call to native function 'SHA1'. The error is only on the SHA1. everything else works fine other then this error. Any helped is greatly appreciated. I don't want the password showing or the encryption that is to stay blank as it is an admin editing a staff members details and they dont need to know the password.
<?php
if(empty($_POST['submit'])) {
$query = "SELECT * FROM user WHERE user_id = '$_GET[id]'";
// $supplier_id = $_GET['supplier_id'];
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
}
else{
$update = "UPDATE user SET
title= '$_POST[inputtitle]',
first_name ='$_POST[inputfirst_name]',
middle_name = '$_POST[inputmiddle_name]',
last_name = '$_POST[inputlast_name]',
gender = '$_POST[inputgender]',
email = '$_POST[inputemail]',
pass = SHA1($_POST[inputSHA1pass]);
car_make = '$_POST[inputcar_make]'
car_model = '$_POST[inputcar_model]',
prefer_car = '$_POST[inputprefer_car]',
car_age = '$_POST[inputcar_age]',
st_no = '$_POST[inputst_no]',
st_name = '$_POST[inputst_name]',
suburb = '$_POST[inputsuburb]',
state = '$_POST[inputstate]',
country = '$_POST[inputcountry]',
postcode = '$_POST[inputpostcode]',
WHERE user_id = $_POST[user_id]";
mysqli_query($dbc, $update) or die(mysqli_error($dbc));
echo "User has been modified!";
header("Location: view_user.php");
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'].htmlspecialchars($_GET[‘id’]); ?>" method="POST">
<input type="hidden" value="<?php echo $supplier = $_GET['id']; ?>" name="supplier_id">
<p>Title:
<input type="text" name="inputtitle" size="20" maxlength="30"
value="<?php echo $row["title"]; ?>"/>
</p>
<p>First Name:
<input type="text" name="inputfirst_name" size="20" maxlength="30"
value="<?php echo $row["first_name"]; ?>"/></p>
<p>Middle Name:
<input type="text" name="inputmiddle_name" size="30" maxlength="100"
value="<?php echo $row["middle_name"]; ?>"/></p>
<p>Last Name:
<input type="text" name="inputlast_name" size="20" maxlength="30"
value="<?php echo $row["last_name"]; ?>"/></p>
<p>Gender:
<input type="text" name="inputgender" size="20" maxlength="30"
value="<?php echo $row["gender"]; ?>"/></p>
<p>Email:
<input type="text" name="inputemail" size="10" maxlength="5"
value="<?php echo $row["email"]; ?>"/>
</p><br>
<p>Password:
<input type="text" name="inputSHA1pass" size="30" maxlength="40"
value="<?php if (isset($_POST['pass'])) echo $_POST['pass']; ?>"/></p>
<p>Car Make:
<input type="text" name="inputcar_make" size="20" maxlength="30"
value="<?php echo $row["car_make"]; ?>"/></p>
<p>Car Model:
<input type="text" name="inputcar_model" size="20" maxlength="30"
value="<?php echo $row["car_model"]; ?>"/></p>
<p>Prefered Car:
<input type="text" name="inputprefer_car" size="20" maxlength="30"
value="<?php echo $row["prefer_car"]; ?>"/></p>
<p>Car Age:
<input type="text" name="inputcar_age" size="20" maxlength="30"
value="<?php echo $row["car_age"]; ?>"/></p>
<p>Street Number:
<input type="text" name="inputst_no" size="20" maxlength="30"
value="<?php echo $row["st_no"]; ?>"/></p>
<p>Street Name:
<input type="text" name="inputst_name" size="20" maxlength="30"
value="<?php echo $row["st_name"]; ?>"/></p>
<p>Suburb:
<input type="text" name="inputsuburb" size="20" maxlength="30"
value="<?php echo $row["suburb"]; ?>"/></p>
<p>State:
<input type="text" name="inputstate" size="20" maxlength="30"
value="<?php echo $row["state"]; ?>"/></p>
<p>Country:
<input type="text" name="inputcountry" size="20" maxlength="30"
value="<?php echo $row["country"]; ?>"/></p>
<p>Postcode:
<input type="text" name="inputpostcode" size="20" maxlength="30"
value="<?php echo $row["postcode"]; ?>"/></p>
<p>
<input type="submit" name="submit" value="Submit"/></p>
</form>
<?php
mysqli_close($dbc);
?>
<br>
<br>
</div>
<br>
<?php
include ('../includes/footer.php');
?>
A few things:
Check that $_POST['inputSHA1pass'] is not empty
Enclose $_POST[inputSHA1pass] in quote marks and curly braces: SHA1('{$_POST[inputSHA1pass]}'). SHA1() expects a string and will error without one
Look into prepared statements. As this code stands, it is wide open to SQL injection attacks as none of your passed data is checked for validity
Change the semi-colon to a comma in the SHA1 line
Finally, once the UPDATE query has been built, capture it and try running it directly on the database server. It should give you a more meaningful error message and make debugging easier

PHP - How can a load page do some of the php

I wish every time I reload or refresh the page. The disabled textbox still disabled. Because my purpose is to modify the textbox once time and then it will be disabled forever. Is this possible?
Below is my code
<tr>
<td><?php echo $lang_txt['leader_id'][$lang]; ?></td>
<td>:</td>
<td><?php if($_POST['txtLeaderID'] == ""){?><input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php }
else
{
?>
<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php
}
?>
</td>
</tr>
Your variant will be worked with
if(!$_POST['txtLeaderID']){echo('<input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')} else {echo('<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')}
For example:
<?php
if (!$_POST['username']){
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
But it didn't work, if we open it in a new tab. You must save this propery anywhere (session, cookie, sql)and check the stored values. it will be work in a new tab.
For example with session:
<?php
session_start();
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = true;
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>

Form Option in PHP to Post to MySQL Database

I have a form in HTML / PHP that posts the information to a MySQL Database. I have ran into a problem now since everything before was just a "input type="text" - and now I want to do a drop down option to select. I am trying but it does not work.
Here is the code on the form: If you need the other PHP file that displays the data and if it will help then please let me know and I will update it.
It is the Select Name = Track that I want to make a drop down. Only thing is the value of the text areas is one set thing in PHP. For the script to work right and post whatever is selected from the drop down, will I have to make each one of those in my Database also ? Please help me out. I cannot figure this one out and make it work. Thank you all in advance!
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>First Name: </strong> <input type="text" name="firstname" value="<?php echo $first; ?>" /><br/>
<strong>Last Name: </strong> <input type="text" name="lastname" value="<?php echo $last; ?>" /><br/>
<strong>Address: </strong> <input type="text" name="address" value="<?php echo $address; ?>" /><br/>
<strong>City: </strong> <input type="text" name="city" value="<?php echo $city; ?>" /><br/>
<strong>State: </strong> <input type="text" name="st" value="<?php echo $st; ?>" /><br/>
<strong>Zip: </strong> <input type="text" name="zip" value="<?php echo $zip; ?>" /><br/>
<strong>Phone Number: </strong> <input type="text" name="phone" value="<?php echo $phone; ?>" /><br/>
<strong>Drivers License: </strong> <input type="text" name="dl" value="<?php echo $dl; ?>" /><br/>
<strong>Email Address: </strong> <input type="text" name="email" value="<?php echo $email; ?>" /><br/>
<strong>Track Name: </strong>
<select name="Track">
<option value="hillclimb">HillClimb</option>
<option value="trioval">Tri-Oval</option>
<option value="pulpfiction">Pulp Fiction</option>
<option value="speeddrome">SpeedDrome</option>
<option value="roadptc">Road PTC</option>
<option value="tubby">Tubby</option>
</select>
<br />
<strong>Lane Color: </strong> <input type="text" name="lane" value="<?php echo $lane; ?>" /><br/>
<strong>Car Number: </strong> <input type="text" name="car" value="<?php echo $car; ?>" /><br/>
<strong>Controller Number: </strong> <input type="text" name="controller" value="<?php echo $controller; ?>" /><br/>
<input type="submit" name="submit" value="Submit">
</div>
</form>
You need to post the PHP code that processes this form result.
My guess is that in PHP, you're using all lowercase (like the rest of your form fields, "firstname", "lastname", "zip", etc..), yet the drop-down for track starts with a capital letter: "Track"

Show confirmation on referring back to previous page (PHP HTTP REFERER)

This might be simple but I am having difficulty figuring it out,
A form with data in index.php is submitted to sell.php which is processed by mysql query and returns automatically to previous page (index.php) after data is stored in database successfully.
The code I am using is:
header("Location: " .$_SERVER['HTTP_REFERER']);
I needed a little enhancement here. When the page sell.php returns back to index.php, it shall give a confirmation message to user that the data was submitted successfully.
index.php
<form name="vender" method="post" action="sell.php">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
<input type="hidden" name="serial" value="<?php echo $identity; ?>" />
<input type="hidden" name="model" value="<?php echo $model; ?>" />
<input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
<table style="font-size: 8pt;">
<tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
<tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
<tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
<tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
<tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
<tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
<tr><td><input type="submit" /></td></tr>
</table>
</form>
sell.php
<?php
include "connect.php";
include "links.php";
$date = $_POST['date'];
$serial = $_POST['serial'];
$model = $_POST['model'];
$imei = $_POST['imei'];
$name = $_POST['name'];
$contact = $_POST['contact'];
$nif = $_POST['nif'];
$qty = $_POST['qty'];
$price = $_POST['price'];
mysql_query("INSERT INTO mobile_sell_data(date,serial,model,imei,name,contact,nif,qty,price) VALUES('$date','$serial','$model','$imei','$name','$contact','$nif','$qty','$price')");
mysql_query("UPDATE mobils SET qty=qty-'$qty' WHERE id = '$serial'");
header("Location: " .$_SERVER['HTTP_REFERER']);
?>
You can't echo anything out once the headers have been sent, since the server is finished dealing with the page when the headers are sent. There are a couple solutions that you can implement here. You could send data back to index using a GET variable, POST variable, SESSION, maybe even a cookie, or you can have the request performed from within the index.php using ajax so that you never actually leave the index page. Here's a simple solution: (Note, you need to remove your redirect in sell.php. Everything takes place in index.php this way)
<?php
$successfulSubmit = FALSE;
if (!empty (#$_POST["sub"]))
{
include "sell.php";
$successfulSubmit = //some logic to verify data was successfully submitted
if ($successfulSubmit)
{
echo "Data submitted successfully";
}
else
{
echo "Data submitted unsuccessfully";
}
}
?>
<form name="vender" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
<input type="hidden" name="serial" value="<?php echo $identity; ?>" />
<input type="hidden" name="model" value="<?php echo $model; ?>" />
<input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
<table style="font-size: 8pt;">
<tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
<tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
<tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
<tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
<tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
<tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
<input type="hidden" name="sub" value="submitted" />
<tr><td><input type="submit" /></td></tr>
</table>
</form>
Have you considered using either a get variable?
header("Location: " .$_SERVER['HTTP_REFERER'] . "?success=true");
Or using a session?
session_start()
$_SESSION['success'] = true
//reset the session on the return page
These aren't particularly elegant solutions, but they will work

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