This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have a simple form that echos a value on submit using form. I want this value to be written to a text file, I cant seem to figure out why my code is not working any ideas?
<?php
if (isset($_POST['button1'])) {
$txt=$_POST['button1'];
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX);
exit();
}
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?> /> Open
<input type="radio" name="button1" value="Closed" onClick="submit();" <?php echo ($_POST['button1'] == 'Closed') ? 'checked="checked"' : ''; ?>/> Closed
</form>
<?php
if (isset($_POST['button1']) == 'Open')
echo "Open Today.";
else if (isset($_POST['button1']) == 'Closed')
echo "Closed Today.";
?>
The form disappears because you exit() before writing anything.
I suggest you to use this code.
on the radio button, I changed
<?php echo ($_POST['button1'] == 'Open') ? 'checked="checked"' : ''; ?>
into
if($_POST['button1'] == 'Open') echo "checked=checked"; ?>
which will avoid misunderstood.
<?php
if (!empty($_POST['button1'])) {
$txt=$_POST['button1'];
file_put_contents('status.txt',$txt,FILE_APPEND|LOCK_EX);
//removed exit
}
?>
<form method="post" action="">
Restaurant Open:
<input type="radio" name="button1" value="Open" onClick="submit();" <? if($_POST['button1'] == 'Open') echo "checked=checked"; ?> /> Open
<input type="radio" name="button1" value="Closed" onClick="submit();" <? if($_POST['button1'] == 'Closed') echo "checked=checked"; ?> /> Closed
</form>
<?php
if ($_POST['button1'] == 'Open')
echo "Open Today.";
elseif ($_POST['button1'] == 'Closed')
echo "Closed Today.";
else
echo "Choose a status.";
?>
Related
I have a problem with php if statement. I want to display propere status user. If user is friend, it should display friend. If user is wainting, should display wainting. And if user not friend and wainting, should display add. This code below, display always only Friend. I don't know why. If someone help me I will gratefull.
<input type="submit" name="addFriend" id="button_color" value="<?php if ($this->status == 'Friend'): ?> Friend <?php elseif ($this->status == 'Wainting'): ?> Wainting <?php else: ?> Add <?php endif; ?>">
You can try this
<input type="submit" name="addFriend" id="button_color" value="<?php if ($this->status == 'Friend'){ echo 'Friend'; } elseif ($this->status == 'Wainting'){ echo 'Wainting';} else{echo 'Add'; ?>">
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have this code for dynamic checked radio box:
PHP:
if ($author === 0) {$checked = 'checked';} else {$checked == '';}
if ($author === 1) {$checked = 'checked';} else {$checked == '';}
if ($author === 2) {$checked = 'checked';} else {$checked == '';}
HTML:
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
<input type="radio" name="test" <?PHP echo $checked; ?> />
This way is true? What’s is a better/optimized Way?
Can I write any PHP function or class for save code and checked any radio box?
It can save you some lines and unnecessary extra variables if you look forward using the shorten if statement form. Example with the first input :
<input type="radio" name="test" <?php echo $author === 0 ? 'checked' : '' ?> />
Ideally you want to check a button if the author value relates to that input, for example:
<input type="radio" name="test" value="0" <?php echo ($author == 0 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="1" <?php echo ($author == 1 ? 'checked="checked"' : ''); ?> />
<input type="radio" name="test" value="2" <?php echo ($author == 2 ? 'checked="checked"' : ''); ?> />
Your currently checking all if the value is any.
The HTML markup is wrong. With that HTML the user is able to select all of those radios. To make it so that the user can only select one of the radio buttons out of that group of options, change the names to all match, like this:
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
<input type="radio" name="test1" <?PHP echo $checked; ?> />
Something you can do:
<?php
$checked = ($author >= 0 && $author <= 2 ? 'checked' : '');
?>
HTML
<input type="radio" name="test" <?php echo $checked; ?> />
<input type="radio" name="test1" <?php echo $checked; ?> />
<input type="radio" name="test2" <?php echo $checked; ?> />
<?php
switch($author) {
case 0:
case 1:
case 2:
$checked = "checked";
break;
default:
$checked = '';
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
i want radio buttons remain selected across all pagination. For taht
I have collected the checked value in pagination at the top of page.
But it is not working well.
What is wrong with this code ?
This is how i get selected value of radio button in session
session_start();
if(isset($_POST['answer'])) {
$_SESSION['answer'] = $_POST['answer'];
}
This is my php code
<input type="radio" name="answer" value="yes" <?php if(isset($_SESSION['answer'])=='yes') {echo "checked"; }?> />
<input type="radio" name="answer" value="no" <?php if(isset($_SESSION['answer'])=='no') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes1" <?php if(isset($_SESSION['answer'])=='yes1') {echo "checked"; }?> />
<input type="radio" name="answer" value="yes2" <?php if(isset($_SESSION['answer'])=='yes2') {echo "checked"; }?> />
It gives me error(notice) like answer is undefined index.
isset() returns true or false. Your if statement should be, for example:
if (isset($_SESSION['answer']) && $_SESSION['answer'] == 'yes')
Try with:
$values = array('yes', 'no', 'yes1', 'yes2');
foreach ( $values as $value ) {
$checked = isset($_SESSION['answer']) && $_SESSION['answer'] == $value ? 'checked="checked"' : '';
echo '<input type="radio" name="answer" value="' . $value . '" ' . $checked . '/>';
}
<input type="radio" name="answer" value="yes" <?php if($_SESSION['answer']=='yes') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="no" <?php if($_SESSION['answer']=='no') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes1" <?php if($_SESSION['answer']=='yes1') { ?> checked="checked" <?php }?> />
<input type="radio" name="answer" value="yes2" <?php if($_SESSION['answer']=='yes2') { ?> checked="checked" <?php }?> />
please use this html
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've got the following in my code and I cant get to work the latter part of the code work. To be more precise on my question, this works the way I want up until I click OneClick. When that's done I get another form (form2) with a TwoClick submit button dynamically populated. The issue comes here. When I click the button TwoClick the whole 'form2' disappears. can some one throw me a clue as to where I have gone wrong?
Thanks.
<?php session_start(); ?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
?>
</div>
</body>
</html>
try this
<?php
session_start();
function putForm2(){
$myForm = "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
return $myForm;
}
?><!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo putForm2();
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{
echo putForm2();
echo "Text box is empty..."; return;
}
}
?>
</div>
</body>
</html>
Session_start() must be called before outputting any output (in the beggining of the script).
Sendtwo form disappears because of your logic - when you submit Sendtwo, $_POST['sendone'] is not set, therefore not being echoed. To fix that you could for example change the first condition to:
if (isset($_POST['sendone']) || isset($_POST['sendtwo']))
try putting
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
at the very end. i mean after the last }
Try this example and read the code comments. Though really its unclear to me what your trying todo so ive just ported code to what I think your trying todo, tho there are better ways handle processing forms. Also You should really think about your form value keys, txt1 and txt123 are not helpful and dont explain the type of variable you want from the form. Perhaps its of interest.
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
$form = null;
//Handle form 1
if(isset($_POST['sendone'])){
//reset session vars if new request
unset($_SESSION['txt1']);
unset($_SESSION['txt123']);
//validate txt1
if(!empty($_POST['txt1'])){
//set into session
$_SESSION['txt1'] = $_POST['txt1'];
//or you could put the value in a <input type="hidden" name="txt1" value="'.htmlspecialchars($_POST['txt1']).'"/>
$form = "
<form name='form2' method='post' action=''>
<table border='1'>
<tr>
<td>form 2 is here!<br></td>
</tr>
<tr>
<td><input type='text' name='txt123' id='txt123'></td>
</tr>
<tr>
<td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td>
</tr>
</table>
</form>";
} else {
$error['txt1'] = 'txt1 is empty!';
}
}
//do second form
if(isset($_POST['sendtwo'])){
//validate
if(!empty($_POST['txt123'])){
//set session
$_SESSION['txt123'] = $_POST['txt123'];
}else{
$error['txt123'] = 'txt2 is empty!';
}
}
//check then do something with both form values
if(empty($error) && isset($_SESSION['txt1']) && isset($_SESSION['txt123'])){
$form = "Both txt1=".htmlspecialchars($_SESSION['txt1'])." and txt123=".htmlspecialchars($_SESSION['txt123'])." was set into session";
}
}
?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="">
<input type='text' name='txt1' id='txt1'>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?php echo isset($error['txt1'])?$error['txt1']:null;?>
</form>
</div>
<div id="two">
<?php echo isset($form)?$form:null;?>
</div>
</body>
</html>
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Below is the code for a simple form with input/output. For some reason, only question one seems to be working and questions two and three simply echo back "Something is wrong." I coded all three questions the exact same way throughout so I'm not really sure why question two and three aren't processing correctly. Any advice would be greatly appreciated.
Here is the HTML form:
<form action="processor.php" method="post">
<h4>Question # 1</h4>
<p>What grade are you in?</p>
<label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
<label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
<label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
<label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>
<h4>Question # 2</h4>
<p>What is your current GPA?</p>
<select>
<option name="gpa" value="4">3.5 or above</option>
<option name="gpa" value="3">3.0-3.4</option>
<option name="gpa" value="2">2.5-2.9</option>
<option name="gpa" value="1">2.0-2.4</option>
<option>Lower</option>
</select>
<h4>Question # 3</h4>
<p>Where do you excel the most academically?</p>
<select multiple="multiple">
<option name="school" value ="1">Mathematics</option>
<option name="school" value ="2">Literature</option>
<option name="school" value ="3">History</option>
<option name="school" value ="4">Humanities</option>
<option name="school" value ="5">Science</option>
</select>
<div class="button">
<button class="btn btn-primary" input type="submit" name="submit" href="processor.php">Submit</button>
</div>
</form>
Here is the processor:
<?php
function Grades () {
$grade = $_POST['grade'];
if ($grade =="1") {
echo "You're a freshmen";
} elseif ($grade == "2") {
echo "You're a sophomore";
} elseif ($grade == "3") {
echo "You're a junior.";
} elseif ($grade == "4") {
echo "You're a senior.";
} else {
echo "Something is wrong.";
}
}
function Gpa () {
$gpa = $_POST['gpa'];
if ($gpa =="1") {
echo "You strongly need to up your GPA.";
} elseif ($gpa == "2") {
echo "You're an average student.";
} elseif ($gpa == "3") {
echo "You're an above average student.";
} elseif ($gpa == "4") {
echo "You're an excellent sudent.";
} else {
echo "Something is wrong.";
}
}
function School () {
$school = $_POST['school'];
if ($school =="1") {
echo "You're into Math";
} elseif ($school == "2") {
echo "You're into Lit";
} elseif ($school == "3") {
echo "You're into history.";
} elseif ($school == "4") {
echo "You're into humanities.";
} elseif ($school == "5") {
echo "You're into science.";
} else {
echo "Something is wrong.";
}
}
include('viewpage.php');
?>
And the view page:
<h4>Question # 1</h4>
<p><?php Grades($grade); ?></p>
<h4>Question # 2</h4>
<p><?php Gpa($gpa); ?></p>
<h4>Question # 3</h4>
<p><?php School($school); ?></p>
The problem is here
<select name="gpa">
<option value="4">3.5 or above</option>
To access $_POST['gpa'] <select> tag should have name property as gpa not <option> tag
For $_POST['School']
<select name="school" multiple="multiple">
<option value ="1">Mathematics</option>
also i think some think wrong in view page code. Grades,Gpa,School function are not have variable. but you call these function like this,
<?php Grades($grade); ?>
<?php Gpa($gpa); ?>
<?php Gpa($School); ?>
replace these code like below
<?php Grades(); ?>
<?php Gpa(); ?>
<?php Gpa(); ?>
and also you will get error if user not select options in question1 n question 2. you must add code to check that..