im creating a form which will display data from database, everything is set so far, but i want the form to remember the data in case if user has to write it down again in case of error. I found some similar code concerning only html, but when including php to display a form i find some difficulties for code to remember the last input (current problem is only concerning drop down selection list):
$Type = $_POST['petType']; //this should remember last input
<?php
/*upload form and drop-down selection list*/
echo "
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>\n
<option value='-1'>Type:</option>";
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
echo "<option value='$petType' ";
if($Type == '$petType')
{
echo "? selected='selected'";
// i need to make selected true only for last selected option,
//and redisplay it in the same form again
}
echo ">$petType </option>";
}
echo "</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
?>
try this:
<?php
$Type = $_POST['petType']; //this should remember last input
?>
<div align='left'>
<form enctype='multipart/form-data' action='ChoosePetCategory.php' method='POST'>
<input type='hidden' name='MAX_FILE_SIZE' value='500000000' />
<input type='file' name='imagePath' size='600' />
<select name='petType'>
<option value='-1'>Type:</option>
<?php
while($row = mysqli_fetch_assoc($PetListResult))
{
extract($row);
?>
<option value='<?php echo $petType;?>' <?php echo $Type == $petType ? "selected=selected":"";?> ><?php echo $petType;?> </option>
}
</select>
<p><input type='submit' name='Upload' value='Add Pet' />
</form>
sidenote: make html and php different , it makes things easier.
You are comparing $Type to a string not a variable.
Try
if($Type == $petType)
Related
I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}
I have a PHP code to display table data with a column of checkboxes used to click to mark the test case as Blocked. I am trying to save the state of checkbox after it is submitted, but I am unable to do so.
Please help!
echo "<form id=\"checkbox\" class=\"check2\" method = \"post\" action=\"\">";
$checked = "";
if(isset($_POST['Blocked[]'])) {
$checked = 'checked="checked"';
}
echo "<td $Blocked><input type =\"checkbox\" name=\"Blocked[]\" value=\"checkblock\" onclick=\"showMsg('div1')\" $checked/></td>";
echo "<input type=\"submit\" value=\"Submit\" class=\"button\" name=\"edit_tc\" onclick=\"myFunction(form)\" style=\"position:fixed; height:25px ; width:150px; bottom:25px; right:200px;\"/>";
echo "</form>";
Firstly, please don't echo the static html like above because it makes the code reading difficult. Secondly, you are using the isset() function incorrectly. Thirdly, your input field name for checkbox is of type array. Do you really need this to be an array?
Please use something like this:
<?php
$checked = '';
if(isset($_POST['Blocked'])) {
$checked = 'checked="checked"';
}
?>
<form id="checkbox" class="check2" method = "post" action="">
<input type ="checkbox" name="Blocked" value="checkblock" onclick="showMsg('div1')" <?php echo $checked;?>/>
<input type="submit" value="Submit" class="button" name="edit_tc" onclick="myFunction(form)" style="position:fixed; height:25px ; width:150px; bottom:25px; right:200px;"/>
</form>
I have two forms created in PHP as below
echo '<form name="delete" action="?page=deletetable" method="POST" autocomplete="off">';
echo '<input type=submit value="Delete" name="'.$row['TableName'].'">';
echo "</form>";
echo '<form name="assign" action="?page=assign" method="POST" autocomplete="off">';
echo '<select name="user">';
$sql = mysqli_query($con,"SELECT * FROM users WHERE hasCustom = 0");
while ($row = mysqli_fetch_array($sql)){
echo "<option value=\"".$row['user_name']. "\">" . $row['user_name'] . "</option>";
}
echo "</select>";
echo '<input type="submit" value="Assign" name="'.$row['TableName'].'">';
echo "</form>";
This looks like the below in the end html code
<form name="delete" action="?page=deletetable" method="POST" autocomplete="off">
<input type=submit value="Delete" name="rates_test">
</form>
<form name="assign" action="?page=assign" method="POST" autocomplete="off">
<select name="user">
<option value="zac">zac</option>
<option value="testadmin">testadmin</option>
<option value="tonyd">tonyd</option>
</select>
<input type="submit" value="Assign" name="">
</form>
as you can see in the last submit button the name tag is empty yet in the first form the name tag is correct i.e name="rates_test"
Both forms are using the same PHP to get this value so I cant see why one works and the other does not.
You're using a 'while' loop to reset $row. My guess is that ['TableName'] doesn't exist in $row after that loop.
Try using separate variable names for your first $row and the one in the while loop.
With the line:
while ($row = mysqli_fetch_array($sql)){
you overwrite the variable $row, try using a different variable for that while loop unless this is intended.
$row variable is getting the new values again when you have fetched the records again from the query.
Try ($row1 = mysqli_fetch_array($sql)) instead of $row = mysqli_fetch_array($sql))
I am new to PHP and having trouble with a user form. The code executes and produces a page with a selection box and a submit button. The submit button should prompt a new php file. However, the code in the new PHP file is not being executed. I'm just getting a blank webpage.
<?php
include 'Connection.php';
echo "<form action=\"accountStatusChange.php\" method=\"post\">";
echo "<br/>";
echo "<select name=\"accountStatus\">";
echo "<option value=\"Active\">Active</option>";
echo "<option value=\"Inactive\">Inactive</option>";
echo "</select>";
echo "<input type=\"submit\" name=\"loadAccountStatus\" value=\"Go\"/>";
echo "</form>";
?>
this is file accountStatusChange.php:
<html><body>
<?php
$status = $_POST['accountStatus'];
echo $status;
?>
</body></html>
First of all, make your life easier, try to change your code to:
<?php
include 'Connection.php';
echo '
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>
';
?>
Then in second file:
<html><body>
<?php
if(isset($_POST['accountStatus'])){
$status = $_POST['accountStatus'];
echo $status;
}else{
echo 'form not submitted';
}
?>
</body></html>
Have you uploaded both files to server?
Are both the files in the same directory?
You don't need to use so many echo statements; though there is no problem with your code.
Try with using $_REQUEST; like this:
$status = $_REQUEST['accountStatus'];
<?php
include 'Connection.php';
?>
<form action="accountStatusChange.php" method="post">
<br/>
<select name="accountStatus">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select>
<input type="submit" name="loadAccountStatus" value="Go"/>
</form>
I have the following code:
<select name="to" class="combo" value='
<?php
if(isset($_POST['reply']))
{
echo "<option value='$reply'>$reply</option>";
}
?>
' />
<?php
$q = $database->selectAllUsersNotMe();
while($row=mysql_fetch_assoc($q))
{
$u=$row['username'];
echo "<option value=\"$u\">$u</option>";
}
?>
</select>
What this does is produce a combo box with a dropdown for all users on my site excluding the user sending the message.
I am trying to add a reply element to the message.
When i click reply, i use the following code:
<? $reply = $_POST['rfrom']; ?>
<form name='reply' method='post' action='/newmessage.php'>
<input type='hidden' name='rfrom' value='<?php echo $pm->messages[0]['from']; ?>' />
<input type='hidden' name='rsubject' value='Re: <?php echo $pm->messages[0]['title']; ?>' />
<input type='hidden' name='rmessage' value='[quote]<?php echo $pm->messages[0]['message']; ?>[/quote]' />
<input type='submit' name='reply' value='Reply' />
</form>
The values are correct and definately pass the information using POST.
On the initial piece of code I provided, how can I alter this so the username that I am replying to is selected when I am replying, if not, the usernames are just listed.
Thanks
$fromname=(isset($_POST['rfrom'])) ? $_POST['rfrom'] : ''; //ought to validate $_POST
while($row=mysql_fetch_assoc($q)) {
$u=$row['username'];
$selected=($u==$fromname) ? 'selected="selected"' : '';
echo "<option value=\"$u\" $selected>$u</option>";
}
$replyUser = $_POST['rfrom'];
while($row = mysql_fetch_object($q))
{
if($row->username == $replyUser)
{
echo('<option value="'.$row->username.'" selected="selected">'.$row->username.'</option>');
}else{
echo('<option value="'.$row->username.'">'.$row->username.'</option>');
}
}