Well i'm submitting form through onChange of select
<form name="selForm" id="selForm" type="POST>
<select name="sel_status" id="sel_status">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
My jQuery Code
$('#sel_status').change(function(){
$('#selForm').submit();
});
And then i'm echoinging the value submitted in php
echo $_POST['sel_status'];
My network tab in Chrome showing 302 status
So how to submit the form through onChange of select and receive those values in php?
<form name="selForm" id="selForm" type="POST">
The action is missing and it's method, not type
You've got an error in your HTML code.
First, there is a " missing (after POST) and this attribute is called method, not type. So the correct code should be:
<form name="selForm" id="selForm" method="post">
<select name="sel_status" id="sel_status">
<option value="one">one</option>
<option value="two">two</option>
</select>
</form>
If your PHP code is in an other file, you also have to set the action="yourfile.php" attribute
Use $_POST['sel_status'];
You cannot use echo $_POST['selForm']; "selForm" is name of the form.
Add form action="yourphpfile.php" and change method="POST".
Try:
echo $_POST['sel_status'];
Related
I am new to php and want to make a dropdown menu where you can select a site that you want, then when you click a button you will be redirected to that site. The button will take you to for example "google.com" if that is what you select, if you select "stackoverflow.com" the same button will take you there. I currently have no working php code as I am not sure where to start. I will include the html code below.
<form method="post" action="testttt.php">
<select id="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
The form ends a bit further down, so please don't complain about there being no :P ANY HELP IS APPRECIATED :)
Try the below code , i have done the example which you want and its work perfectly . You can do it this in single file as i have done .
<?php
if(isset($_POST['btnsubmit']))
{
$options = $_POST["testing"];
header('Location: '.$options.'');
echo "Your option value".$options;
}
?>
<form method="post" action="#">
<select id="mySelect" name="testing" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="http://www.google.com"> Itslearning
<option value="http://www.stackoverflow.com"> NDLA
</select>
<input type="submit" name="btnsubmit" value="GO"/>
</form>
testttt.php will receive values in the $_POST array, so things like 'echo $_POST["somefield"];' will get you data...
...BUT you have to name your form elements. Not '<select id="somefield"...' but '<select name="somefield"...'. you can still have id attributes if you want, but pass to $_POST happens by what you put in the name= attribute.
Once you know the URL you're redirecting to, header("Location: $url");
Hope that gives you the push you need :-)
can you not just use the myfunction function like this:
function myfunction(event){
return window.open(event.target.options[event.target.options.selectedIndex].value,'');
}
If you want to do it in php, your select need a name attribute :
<form method="post" action="testttt.php">
<select id="mySelect" name="mySelect" onchange="myFunction()">
<option value="SelectSite"> Select Site Please
<option value="Itslearning"> Itslearning
<option value="NDLA"> NDLA
</select>
<input type="submit" value="GO"/>
</form>
You can do that either in PHP :
if(isset($_POST['mySelect']) && !empty($_POST['mySelect'])){
header('Location: ' . $_POST['mySelect']);
}
Or in Javascript using the onchange attribute you already set up :
myFunction(){
var target = event.target;
document.location.href(target.options[target.options.selectedIndex].value);
}
I am using multiple select elements on a page. This page's url is something like this: "index.php?s=foo".
Now I want that changing my select boxes will alter my URL and refresh page, this just works partly :(
Here is the example:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
So if I change my selection the url will swap to "index.php?this=1", but I would like to hold the previous _GET Parameters, so change would occur to "index.php?s=foo&this=1".
Any chance I would get that?
Thanks very much for your help.
Best Regards
Using REQUEST_URI does not include your current GET parameters. You will need the function in this gist to transform all parameters into input fields: https://gist.github.com/eric1234/5802030 .
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="get">
<?php array_to_input($_GET); ?>
<select name="this" onchange="this.form.submit()">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
</form>
CAUTION: array_to_input can introduce XSS vulnerabilities. Please escape contents of the $_GET variable so this cannot happen.
You could add each query string item as a hidden field which will be added to the query string when the form is submitted. Be sure to do some sanitizing on these things to make them safe.
if(!empty($_GET))
{
foreach($_GET AS $k => $v)
{
echo '<input type="hidden" name="'.$k.'" value="'.$v.'">';
}
}
The form action can then be hard coded to something like the file name or $_SERVER['SCRIPT_NAME']. Not $_SERVER['PHP_SELF'] - as pointed out by comment below.
edit : posted answer before I was finished. :(
The simple answer is instead of this:
<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">
Do the action like this:
<form action="<?php echo $_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING']; ?>" method="GET">
Which will include the current $_GET values in your form's action/url and by extension be present when the form is submitted. Basically that is the way forms are designed in general to handle submission to a page with query parameters, ie. the default way it works.
Just a note but I think the way you have it now you might get this for your action/url
"http://'.$_SERVER['REQUEST_URI'].'"
It's just a guess, but it looks like the form is html, and the php will just print and not be executed as code ( without php tags ). Probably just an over-site in the example code. But, I would expect this with the format you have:
echo '<form action="'.$_SERVER['REQUEST_URI'].'" method="GET">';
Cheers!
I have a form with action attribute as searchbycat.php. in the form there is a selection field based upon which contents in the php page will be dynamically made. so I am passing the selected value by GET and processing the value in searchbycat.php. so I am implementing jQuery's click event when the user clicks the submit button and changing the action attribute value with the selected value from the drop down menu. but it does not seem to work. I searched for this in stackoverflow and found that it can be done in jquery ajax call. but those answers is not really helping me. can somebody please explain how to do this?
here is my code:
<form action="" id="form1" method="POST">
<select name="category">
<option>Search By Category</option>
<option value="mobile">Mobile Phone</option>
<option value="computer">Computer Electronics</option>
<option value="book">Books</option>
<option value="fashion">Fashion and Beauty</option>
<option value="furniture">Furniture</option>
<option value="house">House Rent</option>
<option value="vehicle">Vehicle</option>
</select>
<input type="submit" name="submit" id="search" value="Search"/>
</form>
and here is the Jquery part I am trying to do:
<script type="text/javascript">
$(document).ready(function(){
$('#search').click(function(){
$('#form1').attr("action","searchbycat.php?category=<?php echo $_POST['category']?>");
});
});
Just change your form to method = 'GET' and action="searchbycat.php"
It will send automatically like you want. You don't really will need the jquery in this situation
When I change the value of the select element, the
Page should load
The value based output should be echoed.
the url Should indicate the http://mysite.com/?sort=value
HTML
<form name="form1" action="" method="post">
<select id="filter1">
<option value="?sort=recent" onselected="this.form.submit();">Most recent</option>
<option value="?sort=views" onselected="this.form.submit();">Most viewed</option>
</select>
</form>
PHP
<?php
if(isset($_GET['filter1']))
{
$term = strtolower($_POST['filter1']);
switch($term)
{
case 'recent':
echo "recent";
break;
case 'views':
echo "by views";
break;
}
}
?>
Try
<form name="form1" action="newurl.php" method="post">
<select name="sort" onChange="this.form.submit();">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
</form>
And remember your form method is post so the sort value will be stored on $_POST['sort']
Also, you can just change the form's method to "get" and leave the form's action value blank, since the default action behavior on post is to call the same file (let's call it form.php) with the get values attached (something like "form.php?sort=selectedvalue"), which already implies an url change, despite being the same file.
You seem to be a little confused about the process of sending data in forms. Here is the code which will work for you, based on your PHP file, and the variables it's looking to get from the querystring.
<form name="form1" action="" method="get">
<select id="filter1" name="filter1" onchange="this.form.submit();">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
<input type="submit" value="Submit" />
</form>
You can see I've changed the method attribute to get. This means the data will be sent via the querystring, so once the form is submit the URL will change to: http://example.com/?filter1=value
Also for the sake of redundancy, you should include a submit button for people who have javascript turned off.
You have to use onChange event on your select object:
<form name="form1" action="" method="post">
<select name="filter1" onchange="parentNode.submit()">
<option value="recent">Most recent</option>
<option value="views">Most viewed</option>
</select>
</form>
The value of var filter1 on your PHP script will be recent or views, no need to use ?sort=whatever to send the data.
Then you must to retrieve the vars from you POST super-global, and not GET as you used. If you want to use GET you must change the method type on your form to get. I think using GET is the right way to do what you want.. so:
<?php
if(isset($_GET['filter1']))
{
$term = strtolower($_GET['filter1']);
switch($term)
{
case 'recent':
echo "recent";
break;
case 'views':
echo "by views";
break;
}
}
?>
I am making an HTML form. I want the results to appear in the PHP script.
<form action="chk_kw.php" method="post"> <br />
<select> name="website_string"
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij/option>
</select>
<input type="submit" name="website_string" >
</form>
The problem is I cannot get the value passed to the PHP. if I use value:
<INPUT TYPE="submit" name="website_string" value="selected" >
It always passes the text in the quotes. In this case "selected". How do I pass one of the strings from the option?
Try this:
<form method="post" action="check.php">
<select name="website_string">
<option value="" selected="selected"></option>
<option VALUE="abc"> ABC</option>
<option VALUE="def"> def</option>
<option VALUE="hij"> hij</option>
</select>
<input TYPE="submit" name="submit" />
</form>
Both your select control and your submit button had the same name attribute, so the last one used was the submit button when you clicked it. All other syntax errors aside.
check.php
<?php
echo $_POST['website_string'];
?>
Obligatory disclaimer about using raw $_POST data. Sanitize anything you'll actually be using in application logic.
<form method="POST" action="chk_kw.php">
<select name="website_string">
<option selected="selected"></option>
<option value="abc">abc</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit">
</form>
As your form gets more complex, you
can a quick check at top of your php
script using print_r($_POST);,
it'll show what's being submitted an the respective element name.
To get the submitted value of the element in question do:
$website_string = $_POST['website_string'];
It appears that in PHP you are obtaining the value of the submit button, not the select input. If you are using GET you will want to use $_GET['website_string'] or POST would be $_POST['website_string'].
You will probably want the following HTML:
<select name="website_string">
<option value="" selected="selected"></option>
<option value="abc">ABC</option>
<option value="def">def</option>
<option value="hij">hij</option>
</select>
<input type="submit" />
With some PHP that looks like this:
<?php
$website_string = $_POST['website_string']; // or $_GET['website_string'];
?>
Assuming you've fixed the syntax errors (you've closed the select box before the name attribute), you're using the same name for the select box as the submit button. Give the select box a different name.
For your actual form, if you were to just post the results to your same page, it should probably work out all right. Try something like:
<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="POST>
Here is what I find works
Set a form name
Use a default select option, for example...
<option value="-1" selected>Please Select</option>
So that if the form is submitted, use of JavaScript to halt the submission process can be implemented and verified at the server too.
Try to use HTML5 attributes now they are supported.
This input
<input type="submit">
should be
<input name="Submit" type="submit" value="Submit">
whenever I use a form that fails, it is a failure due to the difference in calling the button name submit and name as Submit.
You should also set your enctype attribute for your form as forms fail on my web host if it's not set.