submit a disabled input in a form could not get value - php

The form is like below;
<form action="sendmail.php" method="get">
<input type="text" name="phone" id="phone" data-clear-btn="true">
<input type="text" name="name" id="name" data-clear-btn="true">
<input disabled="disabled" type="text" name="textinput-disabled" id="textinput-disabled" placeholder="Text input" value="<?php echo $info;?>">
</form>
$info = "type1"; and the $info works fine in the form.
but In the sendmail.php
$name=$_GET['name'];
$type=$_GET['textinput-disabled'];
$phone=$_GET['phone'];
I get the name and phone, but I can't get the value in the textinput-disabled.
What's the problem here.

Disabled fields are not submitted. You can make it readonly or hidden, to get value when submitted.
<input readonly type="text" name="textinput-disabled" id="textinput-disabled" placeholder="Text input" value="<?php echo $info;?>">

Thats expected behaviour.
Instead use
<input readonly type="text"...
Or if you must use disabled for some reason, add a hidden field:
<input disabled="disabled" type="text" name="textinput-disabled" id="textinput-disabled" placeholder="Text input" value="<?php echo $info;?>">
<input type="hidden" name="hidden" value="<?php echo $info;?>">
$name=$_GET['name'];
$type=$_GET['hidden'];
$phone=$_GET['phone'];

As disabled input cannot be submitted in form so you can use readonly="readonly", so use below :
<input readonly="readonly" type="text"
name="textinput-disabled" id="textinput-disabled"
placeholder="Text input" value="<?php echo $info;?>">
For more information on readonly

I had the same problem, but with a checkbox.
Since the readonly value doesn't change a checkbox so that it cant be clicked, I still had to use the disable option.
So I just added a hidden field with the desired variable name right below the checkbox:
<input type = 'checkbox' value = '1' name = 'EnableD_".$NR."' ";if($noti["ACTIVE"]==1)echo " checked "; echo " disabled >
<input type = 'hidden' value = '1' name = 'Enable_".$NR."' ";if($noti["ACTIVE"]==1)echo " checked "; echo " >

Related

PHP - How can I retain values of textbox array after form submit?

I just have a question.
I have a group of textbox with the same name. (name = "quantity[]")
How can I retain the values of the form after submit? For example, I have 5 textbox with the same name. I filled out 4 of them and submitted the form. I want to display an error that a field is empty and retain the values that I put before I submitted the form.
I can do it if the textbox names are different. But if they have the same, I can't make it work.
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo $_POST['quantity'];?>">
<input type="submit" name="submit">
Thanks for your help php people. Your help is appreciated.
If field quantity is not fixed, that is, it varies. You should use for loop like this
<?php
if(!empty($_POST['quantiy'])){
$count =count($_POST['quantity']);
for($i=0; $i<$count; $i++){
?>
<input type="text" name="quantity[]" value="<?php echo $_POST['quantity'][$i];?>">
<?php
}
}
else
{
?>
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<input type="text" name="quantity[]" value="">
<?php
}
?>
your problem is that you loose the reference to the input fields when the form is submitted. Since all fields have the same name you cannot be sure, which one provided the input.
What you can do: You could populate your form fields by popping the $_POST like so:
<input type="text" name="quantity[]" value="<?php if(!empty($_POST['quantity'])) echo array_pop($_POST['quantity']);?>">
What happens is the following:
upon submission your $_POST-variable gets populated depending on which inputs have any values
after submission at re-rendering the $_POST['quantities'] array gets disassembled, the last element gets removed and it populates the form input
A word of caution: If the user fills out field 1,2 and 4 with this code the fields 1,2 and 3 will be populated.

populate input field with PHP variable

I am creating a pizza ordering site with php. Now I want to echo the variable passed through the URL within the form. I know how to retrieve this data: <?php echo $_GET["numpizzas"]; ?>. But I don't know the proper way to add it to my html form field. any help is much appreciated
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
<?php echo $_GET["numpizzas"]; ?>
I also tried storing the integer in a variable $howmanypizzas = $_GET["numpizzas"]; ?>but it still doesn't show up as the field value.
<?php echo $_GET["numpizzas"]; ?> does not only retrieve the data. echo also outputs it to the html response (the screen)
since you are allready passing your html with an ECHO, you can do:
<?php
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
?>
Explanation: echo is a function that recieves a string and outputs it to html.
So, with the concatenation operator . you can inject the $_GET["numpizzas"] variable into your html, as a string, and pass it to the echo function, wich outputs it to the browser.
Another way to solve it is to only invoke PHP where you need to process your logic, just like #pavithra answer, wich also works.
You're already echoing and trying to echo inside of that. You need to concatenate your variable with the string that you are echoing, see PHP Strings:
echo
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';
You might also consider Heredoc syntax.
<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
but i dont get why do you get this is as a get variable.hope this works
<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>

How do I make check appear in an edit form?

Basically I have a form that I start out with. On that form is the option to select a checkbox and once I click submit on that form, the information is read into a database and the condition on whether the checkbox has been selected now displays a '0' or '1' in the database.
Now I have an edit form. I choose the row I want to edit and my edit form gets populated with what is in the row. The only thing that doesn't show up, is whether that row was 'checked' on the first form. I want the checkbox to be marked if the user checked the checkbox on the first form and visversa.
Sorry if this sounds like a terrible explination.
Code for the first form:
<form method="post" action="processForm.php">
Name: <input type="text" name="names" required = "required"><br>
Activate: <input type="checkbox" name="activateBox"><br>
<input type="submit" value="Create Users"><br>
</form>
And code for edit form:
<form method="post" action="editForm.php">
ID: <input type="text" readonly name="id" value="<?php echo $row['id']; ?>"><br><br>
First Name: <input type="text" name="firstName" value="<?php echo $row['firstName']; ?>"><br><br>
Last Name: <input type="text" name="lastName" value="<?php echo $row['lastName']; ?>"><br><br>
Email: <input type="text" name="email" value="<?php echo $row['email']; ?>"><br><br>
Activate: <input type="checkbox" name="activateBox" value="<?php echo $row['activated']; ?>"><br><br>
<input type="submit" value="Update"><br>
</form>
I tried doing this but it did nothing. I'm sure I'm missing something pretty simple but I can't figure out what.
if (isset($_POST['activateBox'])) {
echo 'checked="checked"';
}
The checked attribute of the <input type="checkbox"> tag should work. Most likely, when you loaded the edit form, it was an HTTP GET request, rather than a POST, so the $_POST['activateBox'] was empty.
In any case, reading the current state on an edit form from the client seems wrong. Shouldn't the current state come from the database?
Try this:
Activate: <input type="checkbox" name="activateBox"<?php if ($row['activated']) echo " checked"; ?>>
Activate:
<input type="checkbox" name="activateBox"
<?php
if($row['activated']==1){
echo ' value="1" checked';
}else{
echo ' value="0";
?>
<br><br>

How to pass a form value to next page in other form

How to pass a form value to next page in other form?
I have this code in the :
<form action="confirm.php" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
Email: <input type="text" name="email">
</form>
Now I want that in confirm.php to put some hidden input fields with this values, I tried this code:
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php $_POST['email']?>">
</form>
And so on in other 2 pages, and in the last page I want to email all this fields, I tried PHP _SESSION, but no luck with that, so I think that this can be more easier for me!
And something else I forgot to tell, on the second page (nextpage.php) action form variable I refer to a file that use this code:
<?php
header('Location: dear-'.$_POST['firstname'].'.php');
?>
<html>
<form>
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
</html>
In this case how to pass this values (firstname, lastname and email) on the next page ? I use that because I want to generate a page like this www.site.com/dear-name.php
Try this in confirm.php
<form action="nextpage.php" method="post">
//some other input fields...
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']?>">
</form>
You forgot to echo your variable.
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
Update:
You have two choices :
Store your data to session and retrieve to another page.
Pass your data using query string.
header('Location: dear-'.$_POST['firstname'].'.php?firstname='.$_POST['firstname'].'&lastname='.$_POST['lastname'].'&email='.$_POST['email']);
use echo:
<input type="hidden" name="firstname" value="<?php echo $_POST['firstname']; ?>">
<input type="hidden" name="lastname" value="<?php echo $_POST['lastname']; ?>">
<input type="hidden" name="email" value="<?php echo $_POST['email']; ?>">
and if this still not working, isset() your post variables:
if(isset($_POST['firstname'],$_POST['lastname'],$_POST['email'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
echo '<input type="hidden" name="firstname" value="$firstname">';
echo '<input type="hidden" name="lastname" value="$lastname">';
echo '<input type="hidden" name="email" value="$email">';
}
In your input text field, value is empty because of echo is missing. You need to add echo to print the value in your form. So that it can be sent into another page.
The second option is that you can use session_start() in top of each page.
and store the value of the variable in your session.
$_SESSION['name'] = "value_of_session"// in your case that is POST data
and this can be used in next page you need not to include hidden fields.
the most important thing is that You must write
<?php session_start() ?> // in each(you've three) page.

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