I am using a get method to select options with years and months. The URL after submitting this selection looks as follows:
www.mywebsite.com/transactions/?yr=2013&mo=6
I can reload this page and the selection is still there. However, when i submit a form to add a record, the page reloads as follows:
www.mywebsite.com/transactions/?#
And the selection is gone. How can I keep the seletion and reload the exact same url after submitting the form?
I am using the following code:
<form action="?" method="post">
<SELECT options with different inputs>
<input type="submit" value="Add">
</form>
In my PHP it looks the following:
header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);
It creates the right URL after submit, only the global variables are not updated. I defined those as follows:
$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');
And they are changed when I select an option.
You could traverse through submitted variables and add them to the HTML code.
<form action="target.php?var1=<?= $_GET["value2"]; ?>&var2=value2" method="get">
Or just use $_SERVER['REQUEST_URI'] inside action="".
Your form element probably has an attribute action="#".
You could replace this with the original request uri (assuming you use the POST method, if you use GET the query parameters in the form's action attribute will be overridden in most browsers.
<form method="POST" action="<?= $_SERVER['REQUEST_URI'] ?>">your form</form>
In your PHP code, after you get the values by GET method, use the header() function as :
header("Location: www.mywebsite.com/transactions/?yr=2013&mo=6");
It will redirect to the page with the form.
EDITED:
This is a sample code for your need:
<?php
$years=array(1999,2000,2001,2002,2003,2004);
if(isset($_POST['year']))
{
/*
Do your coding here
*/
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
if($years[$i]==$_POST['year'])
echo "<option value='{$years[$i]}' selected='selected' >{$years[$i]}</option>";
else
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
else
{
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
?>
It will print the first value of the drop-down list on the first time, and after that it'll save the values.
Inside form tag action attribute should b set to the page which handles your request,i think u have set action='#' .
Please specify more detail.so that i can help u
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 am on page: index.php?page=2 and there is a search form for "crteria" and "term", but every time I submit, it takes me to index.php?criteria=x&term=x instead of index.php?page=2&criteria=x&term=x. So it ignores the page=2. Also I want to submit the criteria and term on the same page I am. Here's the code:
<div id="search">
<form name="search" action="index.php?page=2" method="get">
<p>Search by:</p>
<select name="criteria">
<option>Name</option>
<option>Last name</option>
<option>Project name</option>
</select>
<br><input type="text" name="tern" placeholder="term">
<br><button type="submit">Find`enter code here`</button>
<hr>
</form>
</div>
On the index.php I include the page that the form is on like this:
if(isset($_GET['page']) && $_GET['page'] == 2){
include 'modules/search.php';
}
So when I'm on index.php?page=2 the search.php is providing me with the form that you can see above.
I know, it's a mess, it's my first project. Thank you in advance.
You can simply add an input hidden parameter into your form and remove it from action attribute:
<form name="search" action="index.php" method="get">
<input type="hidden" name="page" value="2">
In case of more than one parameter or if the parameters could vary, you could add this code into the php that print the form:
foreach($_GET as $k => $v){
$k = addslashes($k); //or a DBMS specific escape function
$v = addslashes($v); //or a DBMS specific escape function
echo "<input type='hidden' name='$k' value='$v' />";
}
This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
?>
</select>
<input type="submit" value="Search">
</form>
As you didn't specify an action for your form, the default will be to send the post values to the same page.
See this for more information about action value.
So, in the same page you have the form, you should add a
if(isset($_POST['owner']))
{
// Do some stuff
}
else
{
// Print the form
}
First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:
$_POST["owner"];
$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.
If you
echo $_POST['owner'];//Will display the value of your selected value in the select box.
<form id="search" action="" method="post" >
<select name="owner" id="owner">
<?php
$owner="rejayi"
$sql = mysql_query("SELECT designation FROM designation");
while ($row = mysql_fetch_array($sql)){
if($row['designation'] == $owner){
echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
}else{
echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
}
}
?>
</select>
<input type="submit" value="Search">
</form>
Put Double quotes (") outside and single quotes (') inside
eg:
echo "<option value='".$row['designation']."'>".$row['designation']."</option>";
<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...)
<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.