I have a loop and submit form. I want to insert one query from a variable but when I click on the submit button last one (id number) variable inserts to the database and when I move $_POST['submit'] part inside the while all ID's insert to the database. How can I insert one of the variables from loop?
This is the while part:
<?php
$supql="SELECT * FROM `tbl_users_posts`";
$rez=mysqli_query($conn,$supql);
while ($row=mysqli_fetch_assoc($rez)) {
$psid=$row['id'];
echo $row['post'];
?>
<form id="form1" name="form1" method="post">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php } ?>
And this is the submit part:
if (isset($_POST['submit'])) {
$pid=$_POST['postid'];
$inslik="INSERT INTO t_plik (pid,uid)VALUES('$psid','$uid')";
mysqli_query($conn,$inslik);
}
thanks
<?php
$conn=mysqli_connect('localhost','root','','sitn');mysqli_set_charset($conn,"utf8");
$supql="SELECT * FROM `tbl_users_posts`";
$rez=mysqli_query($conn,$supql);
while ($row=mysqli_fetch_assoc($rez)){
$psid=$row['id'];
echo $row['post'];
?>
<form id="form1" name="form1" method="post">
<input type="hidden" name="postid" value="<?php echo $psid; ?>">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
<?php }?>
<?php
if(isset($_POST['submit'])){
$pid=$_POST['postid'];
$inslik="INSERT INTO t_plik (pid,uid)VALUES('$psid','$uid') ";
mysqli_query($conn,$inslik);
}?>
Your form has no input with name="postid", so $_POST['postid'] wasn't being set. You can send this with a hidden input.
<form id="form1" name="form1" method="post">
<input type="hidden" name="postid" value="<?php echo $psid; ?>">
<input type="submit" name="submit" id="submit" value="Submit">
</form>
The form should still be in the while loop, so you'll get a separate form for each post.
You also have a typo in the code that does the inserting:
$pid=$_POST['postid'];
should be:
$psid=$_POST['postid'];
So you weren't inserting the ID that was submitted.
Related
The only difference is that one form contains the input text and the other one contains the input submit. The reason I am doing this is because I have a large amount of code and I wrote the HTML first placing some input text at the top and the submit button at the end. Here is my code
This is for a project that contains a lot of HTML forms, what is the best practice when having many forms?
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
</form>
<form method="post" action="test.php">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>
It should print the values in $_POST['amount'] if its not empty but instead it prints 'empty' all the time. thanks!
Server Side PHP should look like below:
<form method="post" action="test.php">
<div>
<input type="text" name="amount" id="amount" placeholder="Amount" value="">
</div>
<div>
<div><input type="submit" name="submit" value="Submit"></div>
</div>
</form>
#pteran, that's not it. The positions of the two form do not matter. You are two form, the first one does not have a submit button and the second one have nothing to submit. And least, you can write something in the first form and press on the keyboard and you will see the amount.
You need to group the text field and the button in the same form.
<!-- first form -->
<form method="post" action="test.php">
<input type="text" placeholder="Amount" name="amount">
<input type="submit" name="submit" value="Submit" >
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['amount'])){
echo $_POST['amount'];
}else{
echo 'empty';
}
}
?>
I am trying to delete a row based on its ID
HTML:
<form action="" method="post">
<input type="submit" name="delete" value=" delete "/>
</form>
PHP:
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if(isset($_POST['delete'])){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Nothing happens when the delete button is pressed. How can I fix this?
You need an edit input whose value is the ID your want to delete.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
Replace $id with the actual variable you use in the script that creates the form.
<form action="" method="post">
<input type="hidden" name="edit" value="<?php echo $id; ?>">
<input type="submit" name="delete" value=" delete "/>
</form>
After that try this
$u = $_REQUEST['edit']; // contains the ID of row to be deleted
if($u){
$db->exec("DELETE FROM infos WHERE id = '$u'");
}
Apparently when you are submitting only you posting data to php file.So you can check like this is enough.you told delete not happening so that i am posting this answer
I'm probably dumb, but I read all the questions made here on similar arguments but I couldn't be able anyway to solve my problem (and I'm aware it's a stupid simple problem).
In the page city.php there are two identical forms with two identical linked to the same page overview.php; like this:
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" value="Gallery">
</form>
Since I want to display different contents depending on the button the user presses, I thought to put an unique id like:
<form method="get" action="overview.php">
<input type="submit" id="1" value="Gallery">
</form>
...
<form method="get" action="overview.php">
<input type="submit" id="2" value="Gallery">
</form>
I know it's stupid, but I tried all the methods to get the id, unsuccesfully.
'Cause in overview.php I want to perform a check like:
$choiche = $_GET['id'];
if($choice == '1')
{echo '...display content X...';}
elseif ($choice =='2')
{echo '...display content Y...';}
else
{echo 'Error';}
Thanks in advance!
2 ways:
You can either adjust the form's action:
action="overview.php?id=1"
or you could do something like:
<form method="get" action="overview.php">
<input type="hidden" name="id" value="1" />
<input type="submit" value="Gallery" />
</form>
This will allow you at the next page request to do your code of:
$choice = $_GET["id"];
Since you want to use a Link instead, you can do it by setting up the following line: Gallery
Put name instead of id
<form method="get" action="overview.php">
<input type="submit" name="1" value="Gallery1">
</form>
...
<form method="get" action="overview.php">
<input type="submit" name="2" value="Gallery2">
</form>
You should do something like this:
This is the "Form1.php"
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type1">
<button type="submit">Go</button>
</form>
<form method="post" action="overview.php">
<input type="hidden" id="redirectTo" name="redirectTo" value="Type2">
<button type="submit">Go</button>
</form>
In "overview.php" you should have something like this:
<?php
$redirectTo $_POST["redirectTo"];
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
if($redirectTo == 'Type1'){
?>
<html>Make all you want</html>
<?php
}
?>
is possible to show the 2nd form? after clicked the 1st form submit button? and is there a way to hide the second form?
example
<?php
if(isset($_POST['submit']))
{ #show form 2
}
?>
<form action="" method="post" name="form1">
<input type="submit" name="submit">
</form>
if theres a way to hide form 2
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
thanks in advance fellow web developers
Yes. Move the condition down.
<form action="" method="post" name="form1">
<input type="submit" name="submit">
</form>
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
<?php } ?>
But note that, you have the same name in both the form's submit button, so it is better to disable the previous form's submit:
<form action="" method="post" name="form1">
<input type="submit" name="submit"<?php if (isset($_POST['submit'])) { echo ' disabled="disabled"'; } ?>>
</form>
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="post" name="form2">
<input type="submit" name="submit">
</form>
<?php } ?>
can we use $_POST 2 times with different page ?
this example ..
action.php
<form action="next.php" method="post">
<input name="test" value="test" />
<button type="submit" name="test">submit</button>
</form>
next.php
<?php
$test=$_POST['test'];
?>
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="test1">submit</button>
</form>
next1.php
<?php
echo $test2=$_POST['test1']; >> in here i didnt get test1 value . why ?
?>
Within next.php and action.php you need to change your input type like as
<input type="text" name="test" value="test" />
<input type="text" name="test1" value="<?php echo $test; ?>" />
Within next.php and action.php you were missing the type attr of input tag
and you have same name attributes for submit and input within next.php need to remove or change the value from respective
<input type="submit" value="submit"/>
Below code i have tried my local server and it executed successfully.
action.php:-
<form action="next.php" method="POST">
<input type="text" name="test"/>
<input type="submit" value="submit"/>
</form>
next.php:-
<?php
$test = $_POST['test'];
echo $test;
?>
<form action="next1.php" method="POST">
<input name="test1" value="<?php echo $test; ?>" />
<input type="submit" value="submit"/>
</form>
next1.php:-
<?php
$test2=$_POST['test1'];
echo "------".$test2;
?>
Hope this will help.I think this is achieved your requirement.
If you want to do it within one file e.g. index.php then here is the code
<?php
if(isset($_POST['test'])){
if(isset($_POST['test_text'])){
echo 'Output from NEXT: '.$_POST['test_text'];
}
}
elseif(isset($_POST['test1'])){
if(isset($_POST['test_text1'])){
echo 'Output from NEXT1: '.$_POST['test_text1'];
}
}
?>
<table style="width:100%;">
<tr >
<td style="width:50%;">
<form action="" method="post" name="next">
<input type="text" name="test_text" value="<?php echo isset($_POST['test_text']) ? $_POST['test_text']:''; ?>" />
<button type="submit" name="test">submit test1</button>
</form>
</td>
<td style="width:50%;">
<form action="" method="post" name="next1">
<input type="text1" name="test_text1" value="<?php echo isset($_POST['test_text1']) ? $_POST['test_text1']:''; ?>" />
<button type="submit" name="test1">submit test1</button>
</form>
</td>
</tr>
</table>
I hope this will help.
You need to send the POST value with the second form again, e.g. in a hidden field, otherwise the value won't be sent.
Furthermore your submit button shouldn't have the same name as the input field you want the content from. So change the name of your submit button, e.g.:
action.php
<form action="next.php" method="post"> <input name="test" value="test" /> <button type="submit" name="submit">submit</button> </form>
next.php
<form action="next1.php" method="post">
<input name="test1" value="<?php echo $test; ?>" />
<button type="submit" name="submit">submit</button>
</form>