I am doing project using Zend framework. here within the form I add field using radio buttons. after the form post. it doesn't send that radio button value(but other fields (eg -text field can post)). this is my code in the view.
<form class="custom" method="post">
<?php
foreach ($answers as $answer) {
echo '<input name="q_answer" value="'.$answer.'" type="radio" >'.$answer;
}
?>
<input class="small secondary button" type="submit" value=" Ok ">
</form>
this is my code within controller
if($request->isPost()){
$ans = $_POST['q_answer'];
}
so when I post the form. it gives Undefined index: q_answer error. what is the wrong. please help me.( within the controller I print posted values using var_dump but 'q_answer' value not available)
If no option is selected this field is not appear in $_POST. So you should first check with isset() if it is present and the try to process. And while you are using ZF, you should use getPost()instead of digging directly in $_POST:
$ans = getPost( 'q_answer', 'default-value-if-no-element-is-found' );
Related
I'm creating a page using HTML and PHP that accesses a Marina database(containing boats/owners ETC...), but I dont know how to capture and use the choice selected from the drop down <select> form and then display all the boats under that owners name(on the same page).
Here is my relevant code...
echo '<form align="left"; top="200"; action="page2.php"; method="post">
<p>Select an owner:</p>
<select top="200"; name="form1"; id="form1">';
foreach($values as $v){
echo '<option value="'.$v['LastName'].'">'.$v['LastName'].'</option>';
}
echo '</select>
</form>
<form align="left"; top="250">
<input type="submit" value="Submit">
</form>';
$form1 = $_REQUEST['form1'];
if($form1){//if there is data submitted to the page
echo '<p>$form1</p>';
}
When I use this code I get an error stating "form1 is an undefined index"
My question is how would I capture the name chosen as a variable from the drop down list when the submit button is clicked? (I apologize as I am very new to HTML and PHP and am only posting here because I cannot find a simple or clear answer anywhere else)
The problem is your <input type="submit"> is not part of the same form. Because you create an independent form for the submit button, it only submits that independent form. This independent form does not have the name attribute set, so your $_REQUEST['form1'] will indeed be undefined.
To correct this, simply have the one form, which contains both the selection and submission:
echo '<form align="left" top="200" action="page2.php" method="post">
<p>Select an owner:</p>
<select top="200" name="form1" id="form1">';
foreach($values as $v){
echo '<option value="'.$v['LastName'].'">'.$v['LastName'].'</option>';
}
echo '</select>
<input type="submit" value="Submit">
</form>';
$form1 = $_POST['form1'];
if($form1){ // if there is data submitted to the page
echo '<p>$form1</p>';
}
Note that you also shouldn't have the semicolons separating the HTML attributes; I've removed these. You'll also really want to use $_POST instead of $_REQUEST, as you don't want $_GET access. I've changed this as well.
You also might want to consider extracting the logic from your markup, and separating the two out.
I have a question related to php. If anyone have an idea,please share with me. (I am a beginner in php).
I received a button value from an HTML page and displayed corresponding figure in the second page using code "param1.php". In the same program itself ( ie, "param1.php"), there is a set of radio box and i need to receive the radio value using another php program. But here i have confusion how to receive the radio value using another php program say "param2.php". also how the page redirect (to the php program where i receivvee the radio value) on selecting a radio button.
Thanks
I attach the "param1.php" below,
<!DOCTYPE html>
<html>
<body>
<br>
<?php
$data = $_POST['btn'];
$data2=$data.".png";
?>
<img src="<?php echo $data2;?>">
<h3>SCALE</h3>
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
</body>
</html>
No form tags
There are no form tags, so your radio tags won't get POST'd to param2.php
<form action="param2.php" method="POST">
<input type="radio" name="group1" value="5,10,15" checked> 5,10,15<br>
<input type="radio" name="group1" value="5,9,13"> 5,9,13<br>
<input type="submit" value="Process" />
</form>
Incorrect $_POST key
Now, in your param2.php file, you will be able to get the value of POST['group1']. In your code you're using the wrong key. btn doesn't exist within $_POST (in the code you've given anyway). The radio button name is group1, so access it as such;
$data = $_POST['group1'];
The value of $data will either be 4,10,15 or 5,9,13. Ensure you've got an image named 4,9,13.png and 4,10,15.png else your image won't show. Because they're comma separated, I'm going to assume these are unique file names. So;
foreach( explode(",", $_POST['group1']) as $file) {
echo "<img src='". $file .".png' />";
}
Also, ensure you do some checks on the posted data to validate the user input
I'm trying to get a website working. What I have are basically two images displayed (random, taken out of a mySQL database). What I need to do is (when the user clicks one of the images) the following:
Update the page, passing the info about the selected image (submit form);
Add one piece of data to the database (upvote the image)
I need to use $_POST to pass an array of values to the next page. So I thought:
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image.png"
value ="dat1[\"data1\",\"data2\",\"data3\"]">
<!-- If value must be a single string, I'll use hidden inputs-->
</form>
<form name="input" action="the_page.php" method="POST">
<input type="image"
name="img"
src="image2.png"
value ="dat2[\"data1\",\"data2\",\"data3\"]">
</form>
Then I can upvote the selected image on the mySQL database with a little php upvote() function that updates the record. The upvoting process is done when the new page is loaded. From this, I have a couple questions:
I'm guessing the images will act as buttons, right? (They are supposed to submit the form, hence refreshing the page). If not, how can I achieve this? I'm unable to do it with a link (since I can't add the values to it). Maybe a javascript function? But I don't know how to submit the form that way either...
Once the page is reloaded, does it mean that only the data from one form has been submited, so I can retrieve the data by simply calling the PHP variable $_POST['img'] and get an array back?
EDIT: I now managed to get everything working, slightly similar to what I proposed initially. Thanks for the AJAX suggestion though, since it was what helped me solve it (looked up AJAX tutorials, found solution).
Here's my solution:
<?php
echo "<form name=\"input\" action=\"F2F.php\" method=\"POST\">";
echo "<input type=\"hidden\" name =\"table\" value=\"".$table1."\">";
echo "<input type=\"image\" name=\"nom\" src=\"".$IMG_Route1."\" value =\"".$Nom_base1."\" border=\"0\">";
echo "</form>";
?>
(where the image goes)
and then, on the header:
<?php
if ($_POST['nom']||$_POST['nom_x']){
if (!$_POST['nom']){
echo 'Could not retrieve name. $_POST[\'nom_x\'] = '.$_POST['nom_x']. mysql_error();
exit;
}
if (!$_POST['table']){
echo 'Could not retrieve table. $_POST[\'table\'] = '.$_POST['table']. mysql_error();
exit;
}
upvote($_POST['table'],$_POST['nom']);
}
?>
You can use one form and a set of radio buttons to simplify things a bit. Clicking on the label will toggle the radio button. You can use commas to separate multiple values for each checkbox, which you can then abstract later on (see below)
<form name="input" action="the_page.php" method="POST">
<ul>
<li>
<label>
<img src="whatever.jpg" />
<input type="radio" name="selectedImage" id="img1" value="12,16,19" />
</label>
</li>
<li>
<label>
<img src="whatever2.jpg" />
<input type="radio" name="selectedImage" id="img2" value="12,16,19" />
</label>
</li>
</ul>
</form>
You can detect when the radio button is selected by adding a listener for the change event, then submit the form.
$('input[name="selectedImage"]').change(function() {
$('form[name="input"]').submit();
});
To abstract the multiple values, you can then explode the form result with PHP, which will return an array of the values.
$selectedImageValues = array();
$selectedImageValues = explode(",", $_POST['selectedImage']);
From there you can pull the different values out and save the data to the database.
I am facing a problem with simple validating of radio button though database.
What I want to do is to just simply check whether radio button for each question is selected or not. I know the simple checking. But as I am using php variable name for every radio button name, it's very hard to convert to javascript variable and check. And I am stuck at this point.
<form id="formID" method="post" action="user_anger_quiz.php?rating_finished=true">
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,1"/>
<input type="radio" name="<?php echo $question_form_name; ?>" value="<?php echo $question_id ?>,2" />
<p id="anger_rating_submit">
<input type="submit" value="Rate my anger level"/></p>
</form>
Any suggestions would be appreciated in advanced.
regarding to your comment:
I guess that you will use php to check it due to the fact that you just add php as a tag.
In php you can use $_POST or $_GET on action page of your form to fetch the radio buttons.
Otherwise you can use javascript / javascript library
I think you're asking to make sure there is a radio button checked before submitting? Try the below untested function using jquery to see if there is a radio button selected. Run that before the form is submitted.
function somethingChecked()
{
if (!$("input[name='<?php echo $question_form_name; ?>']:checked").val()) {
alert('You need to check something');
return false;
} else {
return true;
}
}
I am just trying to learn PHP and want to get the value of the textbox using $_post function, but its not working. I am using wamp 2.1 and the code is simple as below
<form method="POST" action="c:/wamp/www/test/try.php">
<input type="text" name="nco" size="1" maxlength="1" tabindex="1" value="2">
<input
tabindex="2" name="submitnoofcompanies" value="GO"
type="submit">
</form>
<?php
if (!isset($_POST['nco']))
{
$_POST['nco'] = "undefine";
}
$no=$_POST['nco'];
print($no);
However in no way I get the value of the textbox printed, it just prints undefined, please help me out.
You first assigned the word "undefine" to the variable $_POST['nco'].
You then assigned the value of the variable $_POST['nco'] (still "undefine" as you stored there) to the variable $no.
You then printed the value stored in the variable $no.
It should be clear that this will always print the word "undefine".
If you want to print the value of the textbox with the name nco, fill out the form with that textbox, and in the page that process the form,
echo $_POST['nco'];
...is all you do.
You need to setup a form or something similar in order to set the $_POST variables. See this short tutorial to see how it works. If you click the submit button, your $_POST variables will be set.
what for you are using this line $_POST['nco'] = "undefine"; } ..?
and please cross check whether you are using form method as post and make sure that your text name is nco ... or else use the below code it will work.
<?php
$no = $_POST['nco'];
echo $no;
?>
<form name='na' method='post' action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type='text' name='nco'>
</form>
thanks
Your action is wrong.
Change it to
action="try.php"