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>
Related
I have got in trouble when I was going to show (print is more suitable) a list of input field within form.
According to normal situation, there will be a Submit button below it in order to submit the form to (any pages).
Then, I was so confused., because when I use this below code to loop.
<table>
<form>
for($a=1; $a<=10; $a++){
<tr>
<td>
<div class="form-group">
<input class="form-control" type="text" name="name<? echo $a; ?>" required="required"/>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" type="text" name="url<? echo $a; ?>" required="required"/>
</div>
</td>
</tr>
}
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</table>
Tadaa...then it shows:
And the Submit button is on the top of the form?!
Can any help me to fix it?
Or...at least some reason?
Update your code like
<tr>
<td colspan="2">
<input type="submit" value="Submit" class="btn btn-primary"/>
</td>
</tr>
it will display submit button below the input field.
You have incorrect order of html tags.
<table>
<form>
<tr>
...
</tr>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
</table>
<table> tag can only contain (in this particular order)
an optional <caption> element,
zero or more <colgroup> elements,
an optional <thead> element,
either one of the following:
zero or more <tbody> elements
one or more <tr> elements
an optional <tfoot> element
Try reordering your elements like this:
<form>
<table>
<tr>
...
</tr>
</table>
<input type="submit" value="Submit" class="btn btn-primary"/>
</form>
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>
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>
This is simple and should work but doesn't, so I'm obviously derping pretty hard somewhere. The uploaded file name should print after form submission.
<?php
if (isset($_POST["submit"])) {
$name_of_uploaded_file = $_FILES['uploaded_file1']['name'];
print($name_of_uploaded_file);
}
?>
<form id="contactform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td>
<table>
<tr>
<td>
Attach Logo:
</td>
<td>
<input type="file" id="uploaded_file1" name="uploaded_file1" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<input name="submit" id="submit" type="submit" value="Send" />
</td>
</tr>
</table>
</form>
You need the content encoding type set on your form open tag.
<form enctype="multipart/form-data" action="uploader.php" method="POST">