Good Day All,
I have a form with two buttons. One to "Edit" and the other one to "Delete" pilot records:
<form method="post" action="<?php echo adminurl('/pilotmanager/pilotsedit');?>">
<table class="PManager">
<tr><th colspan="2">pilot edit form</th></tr>
<tr>
<td><b>First Name:</b></td>
<td><input type="text" name="firstname" value="<?php echo $firstname;?>" /></td>
</tr>
<tr>
<td><b>Last Name:</b></td>
<td><input type="text" name="lastname" value="<?php echo $lastname;?>" /></td>
</tr>
<tr>
<td><b>Airline:</b></td>
<td>
<select name="code">
<?php
$allairlines = OperationsData::GetAllAirlines();
foreach($allairlines as $airline)
{
echo '<option value="'.$airline->code.'" '.$sel.'>'.$airline->name.'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Transfer Hours:</b></td>
<td><input type="text" name="transferhours" value="<?php echo $transferhours;?>" /></td>
</tr>
<tr>
<td><b>Hub:</b></td>
<td>
<select name="hub">
<?php
$allhubs = OperationsData::GetAllHubs();
foreach($allhubs as $hub)
{
echo '<option value="'.$hub->icao.'" '.$sel.'>'.$hub->icao.' - ' . $hub->name .'</option>';
}
?>
</select>
</td>
</tr>
<tr>
<td><b>Total Flights:</b></td>
<td><input type="text" name="totalflights" value="<?php echo $totalflights;?>" /></td>
</tr>
<tr>
<td><b>Total Pay:</b></td>
<td><input type="text" name="totalpay" value="<?php echo $totalpay;?>" /></td>
</tr>
<tr>
<td><b>Pilot active:</b></td>
<td>
<?php
if(intval($pilot->retired) == 1)
{
$retsel='selected';
$activesel = '';
}
else
{
$activesel = 'selected';
$retsel = '';
}
?>
<select name="retired">
<option value="0" <?php echo $activesel?>>Active</option>
<option value="1" <?php echo $retsel?>>Inactive</option>
</select>
</td>
</tr>
<tr>
<td><b>Email Address:</b></td>
<td><input type="text" name="email" value="<?php echo $email;?>" /></td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
<input type="hidden" name="action" value="saveprofile">
<input type="hidden" name="action" value="deletepilot">
<input type="submit" value="Save Changes" style="width:200px;">
<input type="submit" value="Delete" style="width:200px;">
</td>
</tr>
In my module I have the following function:
public function pilotsedit() {
$pilotid = $_POST['pilotid'];
$action = $_POST['action'];
switch($action) {
case 'saveprofile':
$this->savepro($pilotid);
echo '<script type="text/javascript">alert("Profile Updated!");</script>';
$url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
break;
case 'deletepilot':
$this->deletePilot($pilotid);
echo '<script type="text/javascript">alert("Pilot Deleted!");</script>';
$url = $_SERVER['HTTP_REFERER']; // right back to the referrer page from where you came.
echo '<meta http-equiv="refresh" content="5;URL=' . $url . '">';
break;
}
}
I'm trying to separate the button submission using "case" statement in my module but when I submit the form using "Edit" button the record gets deleted. Please help me find out what I'm doing wrong. Thanks
The answer is to use button instead of input tag. What I did was the folowing:
<td colspan="2">
<input type="hidden" name="pilotid" value="<?php echo $pilotid;?>">
<button type="submit" name="action" value="saveprofile" style="width:200px;">Save Changes</button>
<button type="submit" name="action" value="deletepilot" style="width:200px;">Delete Pilot</button>
</td>
And in my PHP module:
public function pilotsedit() {
$pilotid = $_POST['pilotid'];
$action = $_POST['action'];
switch($action) {
case 'saveprofile':
$this->savepro($pilotid);
break;
case 'deletepilot':
$this->deletePilot($pilotid);
break;
}
}
And it worked like I was expecting it to do.
You both send these actions on saveprofile and deletepilot
<input type="hidden" name="action" value="saveprofile">
<input type="hidden" name="action" value="deletepilot">
You should separate them.
Related
I have a HTML/PHP form that needs validation of each of the entered fields before updating MySQL database. It would be nice if this cold be done as each field is entered...however, I could use the form's action attribute to run a different validation script but then if there are errors I would need to return to the form and re-populate it with previously entered data for correction - not sure how I would do all this but I guess it's possible. However, I've seen a post here that shows how this can be done with a javascript function for the validation but is this possible to do in PHP? I'm just learning PHP so I'd hate to now have to also learn javascript :( Some guidance appreciated.
<?php
if(isset($_POST)) {
echo __LINE__ . " Form has been posted <br>";
echo __LINE__ . $_POST['state_city'];
}
?>
<form action="" method="post">
<table border="0" align="center">
<tr height="29px" width = "220px">
<td>
<label for="student_id"><b>Student-ID</b></label>
</td>
<td>
<input type="text" placeholder="Student ID" name="studentid" value="<?php echo $row['studentid'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px">
<td>
<label for="student_name"><b>Student name</b></label>
</td>
<td>
<input type="text" placeholder="Student name" name="name" value="<?php echo $row['$studentname'];?>" required>
</td>
</tr>
<tr height="29px" width = "220px"><td><label for="start_date"><b>Start Date</b></label></td>
<td><input type="date" placeholder="Enter start date" name="start_date" value="<?php echo $row['startdate'];;?>" required></td>
</tr>
<tr height="29px"><td width = "220px">
<b>Student Grade</b></td>
<td>
<select name="sgrade" id="sgrade">
<option value="1" <?php if ($row['$sgrade'] == '1') echo "selected"; ?>>Grade 1<option>
<option value="2" <?php if ($row['$sgrade'] == '2') echo "selected"; ?>>Grade 2<option>
<option value="3" <?php if ($row['$sgrade'] == '3') echo "selected"; ?>>Grade 3<option>
<option value="4" <?php if ($row['$sgrade'] == '4') echo "selected"; ?>>Grade 4<option>
</select>
</td>
<tr>
// value?
<tr height="29px"><td width = "220px"><b>Country of Birth</b></td>
<td>
<select name="country" id="country">
<?php
$sql = "SELECT * FROM countries";
$result = mysqli_query($con, $sql);
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "</option>";
}
?>
</select>
<?php
mysqli_free_result($result);
mysqli_close($con);
?>
</td>
</tr>
<tr height="29px" width = "220px">
<td><label for="state_city"><b>State and/or main city</b></label></td>
<td><input type="text" placeholder="Student State and/or Main City" name="state_city" value="<?php echo $row['$state_city'];?>" required></td>
</tr>
<tr height="29px" width = "220px">
<td><label for="healthcheck"><b>Checked?</b></label></td>
<td>
<input type="radio" id="checkyes" name="check" value="1" <?php if ($row['check']) echo "checked";?>>
<label for="checkyes"> Yes</label><br>
<input type="radio" id="checkno" name="check" value="0" <?php if (!$row['check']) echo "checked";?>>
<label for="checkno"> No</label><br><br>
</td>
</tr>
</table>
<button type="submit">Save</button>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="history.back()" value="Go back!" class="cancelbtn">Cancel</button>
</div>
</form>
<script>
function check() {
document.getElementById("checkyes").checked = true;
}
function uncheck() {
document.getElementById(hcheckyes").checked = false;
}
</script>
</body>
</html>
If you want it to be dynamic as the user enters the information you will have to use JavaScript as that would be a front-end solution. With PHP you can do you validation/sanitizing of the data on the server-side as the form is submitted. It is suggested you do both.
For your question of repopulating this is one way
<input type="text" name="name" value="<?php echo htmlspecialchars($_POST['name'] ?? '', ENT_QUOTES); ?>">
For more info : click here
I have the following form:
<form action="fixedsubmit.php" method="post">
<table class="table-sm" style="width:600px;" >
<tr align="center" >
<?php
foreach ($rows as $x){
?>
<td><b><?php echo $x['name'] ?></b></td>
<?php
}
?>
</tr>
<tr>
<?php
foreach ($rows as $x){
?>
<td><input type text name="amount[]" size="7" style="text-align:center;font-weight:bold;" value="<?php echo $x['amount'] ?>"></td>
<?php
}
?>
</tr>
<tr>
<?php
foreach ($rows as $x){
if ($x['status'] == '1'){
$status = "Active";
}
elseif ($x['status'] == '0'){
$status = "Inactive";
}
else{
echo 'Problem';
}
?>
<td><?php echo $status ?><br />
<?php
if ($status == 'Active'){
?>
Change <input type="hidden" name="status[]" value="0">
<input type="checkbox" name="status[]" value="1">
<input type="hidden" name="id[]" value="<?php echo $x['id'] ?>"></td>
<?php
}
elseif ($status == 'Inactive'){
?>
Change <input type="hidden" name="status[]" value="0">
<input type="checkbox" name="status[]" value="1">
<input type="hidden" name="id[]" value="<?php echo $x['id'] ?>"></td>
<?php
}
}
?>
</tr>
<tr>
<td colspan="10" align="center">
<input type="submit" value="Update Prices" />
</td>
</tr>
</table>
</form>
And the fixedsubmit.php look like this:
// check if form has been submitted
if (isset($_POST['amount'])) {
//var_dump($_POST) . '<br />';
$chkbox = $_POST['amount'];
$i = 0;
while($i<sizeof($chkbox)){
$sql_fixed = "UPDATE fixed_costs SET amount='".$_POST['amount'][$i]."', status='".$_POST['status'][$i]."' WHERE id='".$_POST['id'][$i]."'";
$result_fixed = mysqli_query($con, $sql_fixed);
$i++;
}
}
What I am trying to do is update the status[] fields with either a 1 or a zero. They have to match up to the database id. I know this is simple, but I can't seem to figure it out. If I change it from active to inactive, I want to post a 0 for inactive and a 1 for active.
For this purpose, I solved my problem by just creating a link with the id and status and submitting each one individually.
I have those codes that help to update and delete a row from a table:
html:
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<form action="update_del.php" method="post">
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</form>
</td>
</tr>
<tr>
PHP:
<?php
require_once('../include/global.php');
$id=$_POST['id'];
if(isset($_POST['submit1']))
{
$name=$_POST['med_name'];
$info=$_POST['info'];
$sql = "UPDATE med SET med_name='$name',
info='$info'
WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if($result){
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
if(isset($_POST['submit2']))
{
$sql = "DELETE FROM med WHERE id='$id'";
$result=mysqli_query($con,$sql) or die('Unable to execute query. '. mysqli_error($con));
if(mysqli_affected_rows($con) == 1)
{
header("location:med.php");
}
else
{
header("location:update_false.php");
}
}
?>
Nothing is updated nor deleted. And no errors are shown. If someone could help me, I think the problem is too easy but can't see it where.
You are placing <form> tag at wrong place.
Therefore, your other elements: id, med_name and info are not children of form.
And they are not getting posted.
You should start <form> before table and end after table.
Corrected Code:
<form action="update_del.php" method="post">
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
</tr>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="Update" />
<input class="imgClass_dell" type="submit" name="submit2" value="Delete"/>
</td>
</tr>
</table>
</form>
just do this -
<table class="imagetable" cellpadding="3" cellspacing="1" width="400px" border="1">
<form action="update_del.php" method="post">
<th>ID</th>
<th>Name</th>
<th>Informations</th>
<th>Actions</th>
<tr>
<td><input type="text" name="id" value="<?php echo $rows['id'] ?>"/></td>
<td><input type="text" name="med_name" value="<?php echo $rows['med_name'] ?>"/></td>
<td><input type="text" name="info" value="<?php echo $rows['info']?>"/></td>
<td>
<input class="imgClass_update" type="submit" name="submit1" value="" />
<input class="imgClass_dell" type="submit" name="submit2" value=""/>
</td>
</tr>
</form>
I have a page to update existing recipe records which are in a MySQL database. It brings in the dish details which also includes a separate table of ingredients. However my code only displays one line in the table of ingredients, which I think is the ingredient with the highest ID. I need it to display all the ingredients, but can't work out what needs changing in my code. It gives the user the option to edit and add/delete (using JavaScript) rows if required.
I'm quite new to PHP, so my code may not be efficient but it does update the records. It just won't display all of them in the first instance.
Here's the snippet from my code. The Connection and $DishID are defined earlier on the page.
<table id="Table" border="1">
<tr>
<td>#</td>
<td>IngID</td>
<td>Volume</td>
<td>UnitID</td>
<td>Delete</td>
<td>Add</td>
</tr>
<tr>
<td>1</td>
<?php
$cdquery="SELECT i.IngID, i.IngName, di.Volume, i.UnitID
FROM Ing i
join DishIng di
on i.IngID = di.IngID
Where di.DishID = $DishID";
$cdresult=mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
while ($cdrow=mysqli_fetch_array($cdresult)) {
$IngID=$cdrow["IngID"];
$IngName=$cdrow["IngName"];
$Volume=$cdrow["Volume"];
$UnitID=$cdrow["UnitID"];
}
php?>
<td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
<td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
<td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>
<td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</tr>
</table>
Many thanks in advance.
Bring your input insdie the while loop
and your button outside of while loop like this
echo '<form>';
while ($cdrow=mysqli_fetch_array($cdresult)) {
$IngID=$cdrow["IngID"];
$IngName=$cdrow["IngName"];
$Volume=$cdrow["Volume"];
$UnitID=$cdrow["UnitID"];
?>
<td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
<td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
<td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>
<td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
</tr>
</table>
<?php }?>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</form>
Note mysqli_ does not automatically secure your application, bind your value.
<table id="Table" border="1">
<tr>
<td>#</td>
<td>IngID</td>
<td>Volume</td>
<td>UnitID</td>
<td>Delete</td>
<td>Add</td>
</tr>
< ?php
$DishID = 'put the value here!!!!!!!';
$cdquery="
SELECT
i.IngID,
i.IngName,
di.Volume,
i.UnitID
FROM
Ing i,
DishIng di
WHERE
i.IngID = di.IngID
di.DishID = '".$DishID."'
";
$cdresult = mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
$i = 1;
while ($row = mysqli_fetch_all($cdresult, MYSQLI_ASSOC)) {
?>
<tr>
<td><?php echo $i; ?></td>
<td><input type="text" name="IngID[]" value="<?php echo $row['IngID']; ?>"></td>
<td><input type="text" name="IngName[]" value="<?php echo $row['IngName']; ?>"></td>
<td><input type="text" name="Volume[]" value="<?php echo $row['Volume']; ?>"></td>
<td><input type="text" name="UnitID[]" value="<?php echo $row['UnitID']; ?>"></td>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
<td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
</tr>
< ?php } ?>
I'm trying to build an application using php. in "stock_out.php", i wanna create a form that include two barcodes. I have 8 fields. (1)Effectivedate,(2)Partnumber,(3)Description1,(4)Description2,(5)NPK,(6)Nama,(7)QtyOut,(8)Reason. I wanna using barcode-reader in "Partnumber" and "NPK".
I have some problems:
1. When i press enter, it's respon for button submit.
2. Actually, when i press enter in Partnumber, i want show details data in "description1" and "description2". As well as in NPK, i want show details data in "nama"
3. In the end of form, i still want to submit this form into database.
I'm newbie in AJAX, so i still don't know how to implementation AJAX in PHP. This is my code, I am grateful for the help
<html><body>
<div id="outerContainer">
<?php include("headerFile.html"); ?>
<?php include("navigationFile.html"); ?>
<div id="bodyContainer">
<div id="pageBodyTitle"> Stock Out </div> <br>
<form id="formSearch" name="formSearch" method="post" action="proses_out.php" onSubmit="return cek_data();">
<table id="messageTable">
<tr>
<td class="heading"> Effective Date </td>
<td>
<input class="inputField" name="tgl" type="text" readonly value="<?php echo $_POST['tgl']?>" tabindex="1"/>
<script language='JavaScript'>new tcal ({'formname': 'formSearch','controlname': 'tgl'});</script>
</td>
</tr>
<tr>
<td class="heading"> Part Number </td>
<td><input type='text' name='txtItem' id='txtItem' size="20" class="inputField" value='<?php echo $item;?>' tabindex="2" />
<img src='search.ico' onClick='open_win_item()'> </td>
</tr>
<tr>
<td class="heading">Description1</td>
<td><input type='text' name='txtDesc1' id='txtDesc1' size="50" value='<?php echo $desc1;?>' readonly /></td>
</tr>
<tr>
<td class="heading">Description2</td>
<td><input type='text' name='txtDesc2' id='txtDesc2' size="50" value='<?php echo $desc2;?>' readonly /></td>
</tr>
<tr>
<td class="heading"> NPK </td>
<td><input type='text' name='txtNpk' id='txtNpk' size="20" maxlength="5" class="inputField" value='<?php echo $tr_no;?>' tabindex="3" />
<img src='search.ico' onClick='open_win_npk()'></td>
</tr>
<tr>
<td class="heading">Nama</td>
<td><input type='text' name='txtName' id='txtName' size="30" value='<?php echo $nama;?>' readonly /></td>
</tr>
<tr>
<td class="heading"> Qty Out </td>
<td><input type='text' name='txtQty' id='txtQty' size="5" class="inputField" tabindex="4"/></td>
</tr>
<tr>
<td class="heading"> Reason </td>
<td><select class="inputField" name="choose" id="choose" value="" tabindex="5">
<?php
include "/class/connect_SQL.class.php";
$sql = "select reason_code from Inv_reason_master";
$query = mssql_query($sql);
while ($row = mssql_fetch_array($query))
{
$coba = $row['reason_code'];
?>
<option value="<?php echo $coba; ?>"><?php echo $coba;?></option>
<?php
}
?>
</select></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="Submit" value="Save" class="formButton2" tabindex="6"/>
<input type="reset" name="reset" value="Cancel" class="formButton2" tabindex="7"/>
</td>
</tr>
</table>
</form>
</div>
<?php
if(isset($_GET["message"]) and $_GET["message"]='save')
{
echo "<script language=\"javascript\"> alert('Data Tersimpan!') </script>";
}
?>
<?php include("footerFile.html"); ?>
</div>
You will have to prevent the response of the "Enter"-key. If you are using jquery in your project you can prevent it with this code:
$(function() {
$("form").bind("keypress", function(e) {
if (e.keyCode == 13) return false;
});
});
To bind Enter key to "Part number" field instead you could try this:
$('#txtItem').keypress(function(e) {
if (e.keyCode == 13) {
alert('Enter is pressed in part number');
}
});