Execute input value in different php file [duplicate] - php

This question already has answers here:
PHP keep checkbox checked after submitting form
(5 answers)
Closed 2 years ago.
I'm still studying php html, any help is appreciated.
I have code like this:
<form method="post">
<input type="checkbox" id="option1" name="option" value="<?php echo "Hello World!"; ?>" />
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option" value="<?php echo "Hello Brother" ?>" />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option" value="<?php echo "Hello Human" ?>" />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option"])){
echo "You need to choose at least one!";
}
else {
echo "Print successful!";
}
?>
I want to have function that if checked, then print the value in different php file. I also have problem that when I checked and submit, the check mark disappeared.
I want it to be like, if checked then true, print the value. if not checked then false, do not print the value. Any idea? Thank you very much!

You have to use action="#" in form tag and mentioned the page name.
Working Demo: http://phpfiddle.org/main/code/brkr-u9st
<input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1'])) echo "checked='checked'"; ?> />
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2'])) echo "checked='checked'"; ?> />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3'])) echo "checked='checked'"; ?> />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option1"]) && empty($_POST["option2"]) && empty($_POST["option3"]) ){
echo "You need to choose at least one!";
}
else {
if(isset($_POST["option1"])){
echo $_POST["option1"];
}
if(isset($_POST["option2"])){
echo $_POST["option2"];
}
if(isset($_POST["option3"])){
echo $_POST["option3"];
}
}
?>

You need to have your form action attribute pointed to a PHP file. Then that file will receive the data via POST and you can do whatever you'd like with it there. The action attribute looks like this
<form method="post" action="\path\to\phpfile.php">
Also, the reason the check mark and other values disappear is because when you submit the form, it is currently executing the PHP file that code is written in. Essentially resetting everything. You can retain the input values by passing the POST input values into the HTML. It would look like this
<form method="post">
<input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1']){ echo 'checked="checked"';} ?>/>
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2']){ echo 'checked="checked"';} ?> />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3']){ echo 'checked="checked"';} ?> />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option"])){
echo "You need to choose at least one!";
}
else {
echo "Print successful!";
}
?>
Make sure your input values don't have the same name.

Related

get form values from one php "echo" to the other

i want get the values of form in other php file but i am getting only one input value as the name are same in loop....How to get all the input values with the same name or if i have to change the name tell me how? and how to get it in other php file
<?php
if(isset($_POST['submit'])){
//getting values of number of MCQ questions and true/false questions
$mcq = $_POST['mcq'];
$truefalse = $_POST['truefalse'];
$number = 1;
echo '<form action="finalquestions.php" method="post">';
//loop to get inputs of mcq as many as the user posted
for($i=1;$i<=$mcq;$i++){
echo 'Question:'.$number.'<input type="text" name="question1" /> </br></br>';
echo '<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>
<input type="radio" name="radio1"> <input type="text" name="radiooptions" /> </br>';
echo '</br></br>';
$number++;
}
//loop to get inputs of truefalse as many as the user posted
for($i=1;$i<=$truefalse;$i++){
echo 'Question:'.$number.' <input type="text" name="question2" /> </br></br>';
echo '<input type="radio" name="question2">True</br>
<input type="radio" name="question1">False</br>';
echo '</br></br>';
$number++;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>WebQuiz Generator</title>
</head>
<body>
<input type="submit" name="submit" value="Generate Quiz" />
</form>
</body>
</html>

how to send checkbox value on next page

how to send check box value in to next page without submit button and without action.
here i am going to next page using link so i want that these checkbox value send on next.php
my form is
<form method="post" action="" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
</form>
and here a link by using i am going to next page
go here
To post data from one page to another page you have to use a submit button like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
<input type="submit" value="Go here" />
</form>
also specify receiving page in action attribute of <form>
In another page (next.php).
You have to access your posted values using $_POST['name'];
eg.
echo $_POST['pay_type'];
EDIT
In your code, I can see that you want to send multiple values for checkbox pay_type, for that you have to write it as an array (name="pay_type[]")in HTML to send multiple values.
So, the complete code will looks like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type[]" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" > Monthly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);"> Yearly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);"> Lifetime
<br/>
<input type="submit" value="Go here" />
</form>
Code in next.php:
<?php
echo "<pre>";
print_r($_POST['pay_type']); //it will print complete array of values for pay_type checkbox
echo "</pre>";
?>
Log1c ツ as said before you need a button.. :). What is also possible to use an attribute onClick:
Add an ID at your form tag (id="myForm") (dont forget the target page next.php in action)
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;" id="myForm">
and then the onClick attribute
go here
jsfilleDemo

PHP radio button

Ok so Im trying to use radio buttons in my PHP code. I wrote a Madlib in PHP and I want to write in a radio button that when a user selects it, it will change the story etc. Happy ending or sad ending here the code I have so far. Any help would be appreciated.
<html>
<head>
<title>James Nygaard's Mablib</title>
</head>
<body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
<h1 style="font-family:cursive;">Create your Madlib below:</h1>
<form action="" method="post">
Enter the name of a boy:
<input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
Enter the name of a man:
<input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
Enter the name of a woman:
<input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
Enter your favorite animal:
<input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
Enter a name:
<input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
Enter the name of your favorite city:
<input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
Enter a feeling that ends in "ness":
<input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
Enter a Verb that ends in "ing":
<input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
Enter the name of a boy:
<input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
Enter the name of a girl:
<input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
<input type="submit" value="Click here or press enter to see your MadLib" /><br />
</form>
<div style="color: #2F4F4F; font-family: cursive;">
<?php
$bg = "8FBC8F";
$fg = "2F4F4F";
if(isset($_POST['noun1'])) {
echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
echo "This is where i will put the story. I already have the story but the code is really long so I left it out of this question.";
echo "<br> The end.";
}
?>
</div>
</body>
</html>
It's not especially clear in this small space, but I tried to follow your code style.
Select an option:
<label>
<input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
<input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>
If you run that code you should see what's going on.

Assign HTML attribute with PHP

Hello I'm trying to send one variable with POST (Hello Angel). That is the code:
<form action="dos.php" method="post" name="compra">
<input name="id_txt" type="hidden" value=<?php echo "Hello Angel" ?>/>
<input type="submit" name="Send" value="Send" />
</form>
when in the other page show the variable, only shows up space (only Hello). That is the code:
<?php
if (isset($_POST['id_txt']))
echo $_POST['id_txt']
?>
So, how Can I show all?
Quote your value like this:
<input name="id_txt" type="hidden" value="<?php echo "Hello Angel"; ?>" />
When you don't put quotes around an HTML attribute value it only takes the first word as the value.
You're missing the quotes around the input value
Change:
<input name="id_txt" type="hidden" value=<?php echo "Hello Angel" ?> />
To:
<input name="id_txt" type="hidden" value="<?php echo "Hello Angel" ?>" />

php check if at least two checkbox is checked and display a message to user

<div class='container'><label for='vehicle'>Vehicle:</label><br/>
<span id='contactus_vehicle_errorloc' class='error'></span>
<input type="checkbox" name="vehicle" id="bike" value=<?php echo $formproc->SafeDisplay('bike') ?> /> I have a bike<br />
<input type="checkbox" name="vehicle" id="car" value=<?php echo $formproc->SafeDisplay('car') ?> /> I have a car <br />
<input type="checkbox" name="vehicle" id="motorbike" value=<?php echo $formproc->SafeDisplay('motorbike') ?> /> I have a motorbike <br/>
<input type="checkbox" name="vehicle" id="plane" value=<?php echo $formproc->SafeDisplay('plane') ?> /> I have a plane <br/>
<input type="checkbox" name="vehicle" id="papaki" value=<?php echo $formproc->SafeDisplay('papaki') ?> /> I have a papaki <br/>
</div>
it's simple - sum all the values, and if it's at least two, then you're ok.
Hi To count number of checked checkbox.
replace name="vehicle" with name="vehicle[]". by this change in post value of vehicle an array of checked values will be come.
and then in php file
if(isset($_POST['vehicle'])){
/*check atleast 2 checked*/
if(sizeof($_POST['vehicle']) > 1){
//code comes here
}
}
thanks
PHP doesn't know abotu the IDs, it only knows about the names. Change the name to:
<input type="checkbox" name="vehicle[]" ///>
Then you can check in PHP like this:
<?php
if (count($_POST['vehicle]') >= 2) {
//do something
}
?>
change name like this name="vehicle[]" then in your page
if (count($vehicle) < 2){
echo "please select at least two fields";
}
your code will be like:
Vehicle:
SafeDisplay('bike') ?> /> I have a bike
SafeDisplay('car') ?> /> I have a car
SafeDisplay('motorbike') ?> /> I have a motorbike
SafeDisplay('plane') ?> /> I have a plane
SafeDisplay('papaki') ?> /> I have a papaki

Categories