How do you collect input from a <select> form? - php

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.

Related

How can I get/extract the name value of a submit button?

When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?

How to POST radio button value in php

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' );

$_POST, image forms and mysql.How to get them working together?

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.

Form is not navigating to another page

<form id="enter" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validateForm(this);" >
<p>
<input id="submitBtn" name="submitDetails" type="submit" value="Submit Details" onClick="myClickHandler(); return false;" />
</p>
</form>
<script type="text/javascript">
function myClickHandler(){
if(validation()){
showConfirm();
}
}
</script>
<?php
session_start();
$outputDetails = "";
$outputDetails .= "<table id='sessionDetails' border='1'>
<tr>
<th>Number of Sessions:</th>
<th>{$_POST['sessionNum']}</th>
</tr>";
$outputDetails .= "</table>";
echo $outputDetails;
?>
Above is the code for my form. What I am trying to do is that if the user submits the form, then it will go back to its own page. But if the "SessionNum" equals '1', then instead of posting the form to itself, it should post the form or in other words navigate to the "session_marks.php' page but it is not idng this, if sessionNum equals 1 then it still submits form or navigate back to its own page, what am I doing wrong?
Also lets say it displays a number for the sessionNum and then I submit the form and it submits the form back to itself, the number disappears, how do I keep the number displayed when submitting the form to itself?
Thanks
Where is the conditional logic to change the target of the form post? All I see in the form tag is this:
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
This will always set the form's action to be the current PHP file, not any other PHP file. If you want to conditionally post to a different file, you'll need to add conditional logic in there. Something like this (though there may be better ways to do it, keep in mind that I'm very out of practice with PHP):
action="<?php $_POST['sessionNum'] == 1 ? echo 'session_marks.php' : echo htmlentities($_SERVER['PHP_SELF']); ?>"
As for the number disappearing, I don't see any form element with the name sessionNum. If there isn't such a form element, then there will be nothing in $_POST['sessionNum'], so the number will "disappear" because there's no value to be displayed.
If the above is your actual code, session_start(); has to be placed before ANY other output (html, php's echo, print etc...)

php how to grab the selected value from a drop down list?

<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
i want to grab the selected value and place in in a var
any idea?
thanks
Depends on your form tag.
<form method="post">
Will pass the value to $_POST['gamelist']
While
<form method="get">
Will pass the value to $_GET['gamelist']
Ofcourse, only after hitting the submit button. As morgar stated, this is pretty basic form procession. I doubt you've used google or followed a tutorial, this is almost one of the first things one learn when working with forms and PHP. We arent here to give you full solutions, take this as an example and create the full page yourself:
if($_SERVER['REQUEST_METHOD'] == "Y") {
$choice = $_Y['gamelist'];
// other stuff you want to do with the gamelist value
} else {
echo '<form method="Y" action="file.php">';
// the rest of your form
echo '</form>';
}
Replace Y with either GET or POST.
$choice = $_REQUEST['gamelist']; //works with get or post
$choice = $_POST['gamelist']
if it is a POST, or $_GET if not.

Categories