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
Related
I have a form called addcustomer.php . it's contain firsname, lastname, mobile fields.i have also 2 button on this form. onr button for saving data and the others for sending sms to customers.
i want to when i click second button 3 data fields passed to another form called smsinfo.php.
now action for saving data works good but when direct to smsinfo . there is no data on this form .
<form method ="POST" name = "custform" action="">
<div class="cell_2_2"><label class="lblfields">Firstname:</label> <input type="text" name="firstname" autocomplete="off"/></div>
<div class="cell_3_2"><label class="lblfields">Lastname :</label> <input type="text" name="lastname" autocomplete="off"/></div>
<div class="cell_5_2"><label class="lblfields">Mobile :</label> <input type="text" name="mobile" autocomplete="off"/></div>
<input type="submit" name="submit" value="Save"/>
<input type="submit" name="sendsms" value="Sms"/>
if (isset($_POST['submit']))
{
Saving code here
} else if (isset($_POST['sendsms'])) {
header("Location: smsinfo.php");
}
here code of smsinfo.php:
<?php
$Fname = $_REQUEST['firstname'];
$Lname = $_REQUEST['lastname'];
$Mob = $_REQUEST['mobile'];
?>
<html>
<body>
<form action="sendingsms.php" method="POST">
<input style="width: 100px;" name="firstname" type="text" value="<?php echo $firstname; ?>"/>
<input style="width: 100px;" name="lastname" type="text" value="<?php echo $lastname; ?>"/>
<input style="width: 100px;" name="mobile" type="text" value="<?php echo $mobile; ?>"/>
</form>
</body>
</html>
thanks all and sorry for my poor english
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>
');}
?>
Although variations of this question have been asked before, none of them have helped me solve this puzzle. at present the form I have put together updates the database fine etc, the issue is when I go to edit the field, only the first part of the data is shown for example, if the data was 'Sunny Day' then when i clicked to edit this field all i would get is 'Sunny', so then if i then clicked update, it would edit the database back to Sunny. Am i missing something?!
Thanks in advance.
// Connects to your Database
$query=mysql_connect("localhost", "cl52-abcdef","abcdef") or die(mysql_error());
mysql_select_db("cl52-abcdef",$query);
?>
<html>
<body>
<?php
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['submit']))
{
$holdesc1=$_POST['holdesc1'];
$holdest1=$_POST['holdest1'];
$rrp1=$_POST['rrp1'];
$cpe1=$_POST['cpe1'];
$ea1=$_POST['ea1'];
$query3=mysql_query("update DealOne set holdesc1='$holdesc1',holdest1='$holdest1',rrp1='$rrp1',cpe1='$cpe1',ea1='$ea1' where id='$id'");
if($query3)
{
header('location:list.php');
}
}
$query1=mysql_query("select holdesc1, holdest1, rrp1, cpe1, ea1 from DealOne where id='$id'");
$query2=mysql_fetch_array($query1);
?>
<form method="post" action="">
1 <input type="text" value=<? echo $query2['holdesc1']; ?> name="holdesc1"><br>
2 <input type="text" value=<? echo $query2['holdest1']; ?> name="holdest1"><br>
3 <input type="text" value=<? echo $query2['rrp1']; ?> name="rrp1"><br>
4 <input type="text" value=<? echo $query2['cpe1']; ?> name="cpe1"><br>
5 <input type="text" value=<? echo $query2['ea1']; ?> name="ea1"><br><br>
<input type="submit" name="submit" value="update" />
</form>
<?php
}
?>
</body>
</html>
Only the first part of the data is showing up because your value attribute values do not have quotes around it. Otherwise, your HTML looks like: <input type="text" value=Sunny day name="holdesc1">
Change your text inputs to the following:
<input type="text" value="<? echo $query2['holdesc1']; ?>" name="holdesc1"><br>
<input type="text" value="<? echo $query2['holdest1']; ?>" name="holdest1"><br>
<input type="text" value="<? echo $query2['rrp1']; ?>" name="rrp1"><br>
<input type="text" value="<? echo $query2['cpe1']; ?>" name="cpe1"><br>
<input type="text" value="<? echo $query2['ea1']; ?>" name="ea1"><br><br>
Just some background. I'm using an MVC. My form is put in a div in my template. It posts to the same page where my controller catches it with an isset(), saves stuff to the database, and redirects to a welcome page.
EDIT: It posts everything like it should if I remove the action part of the form. Must be something pertaining to the way index.php reroutes it to ensure that the user has permission to go to that URL, etc. Still looking for a solution.
I've done some debugging and it is apparent that the variables are not showing up in the $_POST variable.
<form method="post" action="<?php echo BASE_URL; ?>/login/register">
<fieldset><legend>Person Info</legend>
<table>
<tr><td><label for="firstname" class="required">Firstname:</label></td>
<td><input name="firstname" id="firstname" type="text" value="<?php echo $firstname; ?>" /></td>
</tr>
<tr><td><label for="lastname" class="required">Lastname:</label></td>
<td><input name="lastname" id="lastname" type="text" value="<?php echo $lastname; ?>" /></td>
</tr>
<tr><td><label for="email">Email:</label></td>
<td><input name="email" id="email" type="text" value="<?php echo $email; ?>" /></td>
</tr>
<tr><td><label for="username" class="required">Username: </label></td>
<td><input name="username" id="username" type="text" value="<?php echo $username; ?>" /></td>
</tr>
<tr><td><label for="password" class="required">Password:</label></td>
<td><input name="password" id="password" type="password" value="<?php echo $password; ?>" /></td>
</tr>
<tr><td><label for="pass2" class="required">Confirm:</label></td>
<td><input name="pass2" id="pass2" type="password" value="<?php echo $pass2; ?>" /></td>
</tr>
</table>
<button type="submit" class="btn btn-custom-lighten">Submit</button>
<button type="button" class="btn"
onClick="parent.location='<?php echo BASE_URL; ?>/nonprofit/welcome'">
Cancel
</button>
<br>
<br>
<button type="button" class="btn btn-primary"
onClick="parent.location='<?php echo BASE_URL; ?>/nonprofits/register'">
Nonprofits Register Form
</button>
<br>
</fieldset>
</form>
You can see my names are consistent from the controller code.
public function register()
{
print_r($_POST);
if (isset($_POST['firstname'])) {
try {
$person = new Person();
$person->handleUpdate($_POST);
$person->setUsername($_POST['username']);
$person->setPassword($_POST['password']);
$person->save();
header('Location: '.BASE_URL.'/login/welcome');
exit();
}
catch (Exception $e) {
$_SESSION['errorMessages'][] = $e;
}
}
$this->template->blocks['content'][] = new Block('registerForm.inc',array('return_url'=>$this->return_url));
}
Any help would be appreciated, but it looks to be an issue with my form. My form does get rendered, it just doesn't submit.
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')?>">