If form is submitted, then checkbox checked - php

I want to make it to so if my form is submitted then then the checkbox should be checked.
Here is what I have:
<input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo checked } ?> />
HTML:
<form id="form1" enctype="multipart/form-data" action="" method="post">
//Stuff here
<input name="submit" type="submit" value="Upload" />
</form>
I am getting a blank page when I test it... Any help will be appreciated.

Blank is because there is an php error of your php code and you have turned error_reporting off.
if($_POST['submit']) should be if (isset($_POST['submit']))
echo checked should be echo 'checked';
The sample below works for me.
<form id="form1" enctype="multipart/form-data" action="" method="post">
//Stuff here
<input name="submit" type="submit" value="Upload" />
</form>
<input type="checkbox" id="chk" <?php if (isset($_POST['submit'])){ echo 'checked'; } ?> />

There is an error in your PHP code.Please correct it.
<input type="checkbox" id="chk" <?php if ($_POST['submit']){ echo 'checked'; } ?> />

Related

I would use $ _POST twice in one page

I have a site where I make a payment, a bill is created through it, but the problem is that I can not use $ _POST twice in one, and this example:
<? if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<? } if (isset($_POST['pay'])) {
// MY QUERY HERE
// HEADERS HERE
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<? } ?>
Try this.
Kindly check the code for comment.
<?
if (isset($_POST['submit'])) {
$info = $_POST['info'];
// use echo to display second form
echo '
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<!-- // Note the action value, it's for post back. This allow you to post back to the current page -->
<!-- This is to keep the record passed from the first form -->
<input name="info" value="'. $info .'" type="hidden" />
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>';
} else if (isset($_POST['pay'])) {
// MY QUERY HERE
} else {
// Note the action value, it's for post back. This allow you to post back to the current page
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
}
?>
Not the prettiest way to do this but to achieve your result try this...
<?php if (isset($_POST['submit'])) { ?>
<form action="" method="POST" >
<input name="invoice" value="" type="text" />
<input name="pay" value="Pay Now" type="submit" />
</form>
<?php } elseif (isset($_POST['pay'])) {
// Perform query here
} else { ?>
<form action="" method="POST" >
<input name="info" value="" type="text" />
<input name="submit" value="Submit" type="submit" />
</form>
<?php } ?>

php mysql - insert one query from a while loop

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.

Is there a way to show a form after clicked the first submit on first form?

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 I use $_POST 2 times with different page?

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>

understanding sessions

I've written this very simple bit of code. but when I click log out, nothing should happen to the session, yet it is no longer displayed.
please help me understand why.
thanks.
<?php
echo <<<_END
<form method="post" action="">
<input type="hidden" name="in" value="yes" />
<input type="submit" value="Log in" /> </form>
<form method="post" action="">
<input type="hidden" name="out" value="yes" />
<input type="submit" value="Log out" /> </form>
_END;
if(isset($_POST['in']))
{
session_start();
echo "hello, logged in!";
}
if (isset($_POST['out']))
{
echo "logged out";
}
echo session_id();
?>
You can't have a session ID if you don't start the session.

Categories