I am in the midst of completing a uni assignment and ive come across the issue of being totally stumped on how to update and delete data in my database through the use of a form.
I have successfully connected the database so that when the user selects the the addressID in the previous page they wish to work on, it will take them to the updateform.php page.
My code for the form is as follows:
<form method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" class="form-control"/></td>
</tr>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" class="form-control"/></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city" class="form-control"/></td>
</tr>
<tr>
<td>State</td>
<td><input type="text" name="state" class="form-control"/></td>
</tr>
<tr>
<td>Zip</td>
<td><input type="text" name="zip" class="form-control"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>
</tr>
I am now completely stumped on how to connect the data they enter in the form to update the database.
I am trying to update the addresses table with the selected addressID chosen earlier if that helps.
Any push in the correct direction will be greatly appeciated.
Kind Regards
Change these line:
<form method="post">
to
<form method="post" action="process.php">
and
<input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/>
to
<input type="submit" name="update" value="Update" class="btn btn-success btn-lg"/>
and
<input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/>
to
<input type="delete" name="delete" value="Delete" class="btn btn-success btn-lg"/>
process.php:
if(isset($_REQUEST['update']))
{
// update block
// get all required value and fire update query
}
if(isset($_REQUEST['delete']))
{
// delete block
// get all required value and fire delete query
}
You could try something like this:
updateform.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
strip tags from user input:
$firstline= trim(strip_tags($_POST['first_line']));
$secondline= trim(strip_tags($_POST['second_line']));
create query:
$myquery = "INSERT INTO tablename (firstline, secondline)
VALUES ($firstline', '$secondline')";
execute query (confirm success/failure if/else):
if (#mysqli_query($database, $myquery)) {
print '<p>Entries accepted</p>';
}
else {
print '<p style="color: red;">Could not INSERT values because:<br />'
. mysqli_error($database) . '</p>;
}
}
?>
Then have your form post values for the query variables:
<form action="updateform.php" method="post">
<tr>
<td>Firstline</td>
<td><input type="text" name="firstline" size="20" value= <?php if (isset($_POST['firstline'])) {
print htmlspecialchars($_POST['first_line']);
} ?> /></td>
<tr>
<td>Secondline</td>
<td><input type="text" name="secondline" size="20" value= <?php if (isset($_POST['secondline'])) {
print htmlspecialchars($_POST['second_line']);
} ?> /></td>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Update" class="btn btn-success btn-lg"/></td>
</tr>
<tr>
<td></td>
<td><input type="delete" name="submit" value="Delete" class="btn btn-success btn-lg"/></td>
Related
I'm a PHP beginner & have a question xd
Whenever I click on the submit button, the data of the last user in the PHP table are changed.
Could somebody check on it?
<?php
foreach ($db->results() as $unpaid){
?>
<form method="POST">
<tr><td>
<?=$unpaid->id?>
<input type="hidden" name="user" value="<?=$unpaid->id?>">
</td>
<td><?=$unpaid->username?>
</td>
<td><?=$unpaid->bitcoinadress?></td>
<td><?=$unpaid->points?></td>
<td><?=$unpaid->requestdate?></td>
<td><?=$unpaid->status?></td>
<td>
<input type="submit" class="btn btn-warning" name="submit" value="Submit" /><br/>
</td>
</tr>
<?php } ?>
<?php
if(isset($_POST['submit']))
$id = $POST_['user'];
$db->update("payment_request", $id, ["status"=>"Paid"]);
?>
</form>
</table>
You can't put a <form> around a <tr>. You need to put the form inside one of the <td> tags.
<table>
<?php
foreach ($db->results() as $unpaid){
?>
<tr>
<td> <?=$unpaid->id?> </td>
<td><?=$unpaid->username?> </td>
<td><?=$unpaid->bitcoinadress?></td>
<td><?=$unpaid->points?></td>
<td><?=$unpaid->requestdate?></td>
<td><?=$unpaid->status?></td>
<td>
<form method="POST">
<input type="hidden" name="user" value="<?=$unpaid->id?>">
<input type="submit" class="btn btn-warning" name="submit" value="Submit" /><br/>
</form>
</td>
</tr>
<?php } ?>
<?php
if(isset($_POST['submit']))
$id = $POST_['user'];
$db->update("payment_request", $id, ["status"=>"Paid"]);
?>
</table>
I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
However, I do not want to use a form action. Is it possible to do it
without creating form action with PHP?
No, that's not possible.
Just do it like this:
<tr>
<form method="post" action="mypage.php">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<?php
if(isset($_POST['finish_product']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
To enter data into my database I created this form. The user can enter data add by clicking the add button the information is entered into the database. I am using required to force the user to enter text.
Now I added the edit and delete button. Therefore the user shall select a record with the radio buttons provided. But with the required in the input elements it is only possible to do edit or delete if text is entered.
Is it somehow possible to assign the required to a certain button?
<form>
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input type="text" placeholder="enter" required >
</td>
<td>
<input type="text" placeholder="something" required>
</td>
</tr>
</table>
<input type="submit" value="add" formaction="form1.php" formmethod="post"/>
<input type="submit" value="delete" formaction="form2.php" formmethod="post"/>
<input type="submit" value="edit" formaction="form3.php" formmethod="post"/>
</form>
A way to do this, is without using 'required', but reproducing this html5 effect using event handlers, focus() and alert().
event.preventDefault() prevents the form to submit.
Check it:
<form method="POST">
<table>
<tr>
<td>
<input type="radio" name="aName" />
</td>
<td>a</td>
<td>record</td>
</tr>
<td>
<input type="radio" name="aName" />
</td>
<td>another</td>
<td>record</td>
</tr>
<tr>
<td></td>
<td>
<input id="enter" type="text" placeholder="enter" >
</td>
<td>
<input id="something" type="text" placeholder="something" >
</td>
</tr>
</table>
<input id="add" formaction="form1.php" formmethod="post"
onclick="
if(document.getElementById('enter').value===''){
event.preventDefault();
document.getElementById('enter').focus();
alert('Please,fill out this field');
} else if(document.getElementById('something').value===''){
event.preventDefault();
document.getElementById('something').focus();
alert('Please,fill out this field');
}"
type="submit" value="add"/>
<input id="delete" type="submit" formaction="form2.php" formmethod="post value="delete"/>
<input id="edit" type="submit" formaction="form3.php" formmethod="post value="edit"/>
</form>
i am designing a login page using html and php.it includes two buttons one for login and another one to redirect to the registration page.i am planning to use php code to validate login data on the same page.here is my code
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<div>
<table style="width: 100%; height: 377px;">
<tr>
<td rowspan="4"><img src="woman.jpg" height="100%" width="100%" alt="woman"/></td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white" >username:</td>
<td><input runat="server" name="Text1" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="right" style="padding:0px;color:white">password</td>
<td><input runat="server" name="Text2" type="text" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td></td>
<td ><input name="Submit1" type="submit" value="login" />
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="submit" value="register" /><br/>
forgot password</td>
</tr>
</table>
</div>
</form>
but the button for registration is not working properly.it is not redirecting to the registration page.i don't want to put register button outside the form.i want to keep the same design and achieve required functionality.
Change the Button-Type from type="submit" to type="button" to disable the form submission:
<input onclick="document.location.href='regforswa.php'" name="Submit2" type="button" value="register" />
This form has multiple tbody in one form.
Each tbody contains "hidden" field with "unique" value.
However, when this form is sent, it only sends the last "pouzivatel_id" value.
e.g.
I want to execute "pouzivatel_id"=4, but the form sends "pouzivatel_id"="1"
How would I fix this without using multiple forms?
<form method="post" action="lib/action/admin.post.php">
<table>
<thead>
<tr>
<th>Osobné číslo</th>
<th>Prezývka</th>
<th>Meno</th>
<th>Priezvisko</th>
<th>Operácie</th>
</tr>
</thead>
<tbody>
<tr>
<td>00000000004</td>
<td>user4</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="4"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000003</td>
<td>user3</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="3"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000002</td>
<td>user2</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="2"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td>00000000001</td>
<td>user1</td>
<td>user</td>
<td>user</td>
<td>
<input type="hidden" name="pouzivatel_id" value="1"/>
<button type="submit" name="ziadatel-schvalit" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat" class="tlacidlo cervena">Zamietnuť</button>
</td>
</tr>
</tbody>
</table>
You could use a different name for every submit button. If a submit button is pressed, only this submit button is sent to your PHP script. In your PHP script you check which button was pressed.
Example HTML code:
<input type="hidden" name="pouzivatel_id[]" value="2"/>
<button type="submit" name="ziadatel-schvalit_2" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_2" class="tlacidlo cervena">Zamietnuť</button>
<input type="hidden" name="pouzivatel_id[]" value="1"/>
<button type="submit" name="ziadatel-schvalit_1" class="tlacidlo zelena">Schváliť</button>
<button type="submit" name="ziadatel-zmazat_1" class="tlacidlo cervena">Zamietnuť</button>
Example PHP code:
<?PHP
foreach($_POST['pouzivatel_id'] as $id)
{
if(isset($_POST['ziadatel-schvalit_' . $id]))
{
//Schváliť
}
elseif(isset($_POST['ziadatel-zmazat_' . $id]))
{
//Zamietnuť
}
}
?>