Extract html input with php - php

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

Related

Get selected option from html via php

I have a simple simple question, And is so strange for me.
In my HTML file I have a button and a form tag in which used select tag with some options as :
<form action="" method="post" name="general"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select> <br>
</form>
<form action="" method="post" name="apply">
<input type="submit" name="apply" value="Apply"/>
</form>
Include that HTML file in my Php file, create a class and a function to display the selected option of the HTML as below :
<?php
include('./view/config.html');
class SSHCommand{
public function display(){
if (isset($_POST['apply'])) // press button {
$category = $_POST['wirelessmode']; // get the select option
echo $category;
}
}
}
$sshCommand=new SSHCommand();
$sshCommand->display();
?>
When i try, it gives me nothing !
But when i try :
echo 'david';
instead of
echo $category;
It prints david after press apply button.
Where I am doing wrong?
you can get a result using single FORM, modified your HTML code as per bellow.
<form action="" method="post" name="general" id="general_form"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select>
<input type="submit" name="apply" value="Apply"/>
</form>
You would have to include your config.html as html with a php extension .. config.php for the html include to work.. that would be a key thing.
You can include HTML or text in a PHP include file. Anything that can go in an standard HTML file can go in a PHP include.
Your entire website should be subsequently saved using .php extensions however, whether they include php or not, that's the downside, eg. index.php rather than index.html, so it could be time-consuming. Some servers don't require this, so test your configuration first.
Best of luck
Your select is in one form block and the submit button is in another. The default nature of the submit button will submit only the html elements in that form and not all forms of the page. So to get this to work, you need to keep one form with the required elements as
<form action="" method="post" name="general"> Wireless mode
<select name="wirelessmode" id="wirelessmode">
<option value="ap">AP</option>
<option value="client">Client</option>
<option value="clientbrdige">Client Bridge(Routed)</option>
<option value="adhoc">Adhoc</option>
<option value="wdsstation">WDS Station</option>
<option value="wdsap">WDS AP</option>
</select> <br>
<input type="submit" name="apply" value="Apply"/>
</form>

How to get link url when using select box form method get?

I have a sample code:
<form action="index.php" method="get">
<select name="id">
<option value="1">One</option>
<option value="2">Two</option>
</select>
<input type="submit" value="submit" name="submit" />
</form>
When I submit form is url is "index.php?id=1" // or index.php?id=2
=> How to fix it is result "index.php?id=1&name=One" // or index.php?id=2&name=Two
I think the best approach is to populate the options from a db. Use the values as db ID numbers. Then you can store as much info as you want for each option. example:
//some sort of loop{
<option value="
<?php echo $row_option['id'];?>">
<?php echo $row_option['display_text'];?></option>
}
At the other end query db with value; use whatever data need with that value.

Simple PHP select option programming

I have a simple problem in php select option.
The page has 5 select option & submit button. When I select a option then it should goes on specific web page. For Example: http://onlinetools.org/tricks/using_multiple_select.php
Then I select a option & press Send then It show Which option I select. But I need go specific webpage.
I have tried with this code but I have failed...
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test[]" multiple="multiple">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
<?php
$test=$_POST['test'];
echo "
<script type=\"text/javascript\">
window.location = \"$test\"
</script>
";
?>
Anybody Can help me?
This should be a simple select not a multiple one since you want to redirect to only one site. here is the code :
<?php
$test=$_POST['test'];
if(isset($test)){
header("Location: $test");
}
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<select name="test">
<option value="http://google.com">one</option>
<option value="http://yahoo.com">two</option>
<option value="http://facebook.com">three</option>
<option value="http://who.is">four</option>
<option value="http://myspace.com">five</option>
</select>
<input type="submit" value="Send" />
</form>
As you've declared:
<select name="test[]" multiple="multiple">
test is an array, thus you have to take the first object of the array or remove the unused []. By the way, you can do an HTTP redirection, which is faster and works more often than javascript ones.

Append form value to URL that already has variables in it

I've got a page showing the results of a MYSQL query written in PHP. The URL has the variables that the user submitted on the previous page as:
www.mydomain.com/search/?var1=xx&var2=xx&var3=xx
When the user is on the results page they need to be able to sort the results. To do this I've got a SELECT form
<form action="/search<?php echo $urlQuery; ?>" name="order "class="formsrch" method="post" >
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
The variable $urlQuery contains the string to be appended onto the url:
i.e. $urlQuery = "?var1=xx&var2=xx&var3=xx"
The problem is that when the form is submitted the page is reloaded and at the end of the url is ?order=dist.
Is there a way of replacing the question mark with an ampersand so the page will load and the value of order can be retreived?
Or, if anyone has a better way of doing the whole thing I'm definitely open to suggestions.
Thanks
why don't you put them in the form as hidden?
<?php
$extraVars = "";
foreach($_GET AS $key=>$value) {
$extraVars .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />';
}
?>
<form action="/search" name="order "class="formsrch" method="post" >
<?php echo $extraVars;?>
<label>sort by:</label>
<select class="order" id="order" name="order" onChange="this.form.submit()">
<option value="pricedesc">Price High to Low</option>
<option value="priceasc">Price Low to High</option>
<option value="dist">Distance</option>
</select>
</form>
You could make a hidden field for each of the variables you want to transfer so that they get appended too.
Better still, make the method of the form get and then access all the variables with the $_REQUEST global in the backend script.
Output the other variables as <input type="hidden" name="var1" value="xx" /> instead of as the form action, so they'll get taken into your query string.
With this action="/search" and $urlQuery = "?var1=xx&var2=xx&var3=xx"
then to have the value appended on the querysting you should change the form method to "GET".
If you want to keep the form method to "POST" then some javascript would be required to change the action of the form when the form is submitted.

HTML form submit to PHP script

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.

Categories