I am working on a script to edit a file on my vps, so far thanks to the help from a user here I have the following:
<?php
if(!empty($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /home/user/colors/color.choices");
echo 'File color choice has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="red">red</option>;
<option value="blue">blue</option>;
<option value="black">black</option>;
<option value="orange">orange</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
This changes the values as needed but I have one issue I am still trying to solve. The script does not get the current value that is in the file itself, so when I first visit the page it always says "red". After I make a change it still says "red" on the php form.
Edit: I would like the form to always display the current value in the color.choices file, instead of just going back to "red" every time. If I change the value on the form to "blue" I want the form to display that the current value in the file is set to "blue".
I have been told I need to use "file_get_contents" to first retrieve the value from the file itself. I have made a few attempts but I am getting no where. If I need to add more information please let me know!
What if you did this?
<?php
$current = file_get_contents("/var/www/html/colors/color.choices");
$color = explode("=", trim($current));
if(!empty($_REQUEST['color_choice'])){
exec('sed -i '.escapeshellarg('s/color=.*/color='.$_REQUEST['color_choice'].'/g')." /var/www/html/colors/color.choices");
echo 'File color choice has been updated';
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="color_choice">;
<option value="red" <?php if($color[1] == 'red'){?>selected="selected"<?php }?>>red</option>;
<option value="blue" <?php if($color[1] == 'blue'){?>selected="selected"<?php }?>>blue</option>;
<option value="black" <?php if($color[1] == 'black'){?>selected="selected"<?php }?>>black</option>;
<option value="orange" <?php if($color[1] == 'orange'){?>selected="selected"<?php }?>>orange</option>;
</select>
<input type="submit" name="Submit" value="Submit" />
</form>
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);
}
Here is my code. This needs some optimization if you think the below code is bad and someone can really help me
I'm using two if condition and two else condition. Is there anyway this can be fine-tuned.
In the search box i need to retain the previous selected book name. i.e when the submit button is clicked the select box should display the result with the
selected item in the select box. for ex: if in dropdown if i select "book1" and click the submit button, the result should be displayed along with the "book1" as default inside the select box. in my case, the search box is getting reset on each submit click.
My Code:
<?php
if(isset($_POST['submit'])){
$get_project_id = $_POST['project_name'];
if($get_project_id == 1){
..display all the books(mysql query)..
}else{
..display the particular book(mysql query)..
}
}else{
..if the search button is not clicked then display all the books(mysql query)..
}
?>
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Any help will be appreciable.
Than,ks
Kimz
You can do it by checking the condition inside while loop. Checking the value of option and the post value are same or not as shown in the below code.
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option <?php if(isset($_POST['book_list']) && ($_POST['book_list'] == $row['Project_Auto_Id'])) { echo 'selected'; } ?> value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Not sure if there is any better way to do the two if... else statements but even if there is there's nothing wrong with the way you've done it, just a different way.
With regards to having the option selected after submitting you need to change your output of the options to this:
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>"<?php if(isset($_POST['book_list']) && $_POST['book_list'] == $row['Project_Auto_Id']) echo ' selected'; ?>>
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
Is it possible that when
I select something from the list
And I click Go
Browser will remember my last choice from the list
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<option value="">select</option>
<option value="client">table client</option>
<option value="user">table user</option>
</select>
<input name="submit" type="submit" value="go" />
</form>
For example,
if I chose from the list user table
And hit Go
He will remain there
you can use sessions to access some data at a later time on a different script:
session_start();
$_SESSION = $POST['list'];
now the selected item from the list will remain there, as long as the browser is still opened.
if you want to remove the item from the session use:
unset( $_SESSION['list'] );
and remember every time you use sessions you must use at the top of the file (always):
session_start();
In order to make a select "stick" you need to put the word selected inside the <option> tag before the value. In order to do that you’re going to need to dynamically generate the options using a foreach loop. Inside the loop you’re going to look at the $_REQUEST and if it matches the value of what is being iterated in the loop you echo selected.
I’ve done this a dozen or more times and it works perfectly.
<?php
$options = array("select", "client", "user");
?>
Then in the page:
<form action="<?php echo $_SERVER ['PHP_SELF']; ?>" method="get" name="search_frm" id="serch_frm">
<input name="serchStr" type="text" />
<select name="list">
<?php foreach ($options as $option): ?>
<option <?php if ($_GET['list'] == $option) { echo "selected"; } ?> value="<?php echo $option; ?>"><?php echo $option;?></option>
<?php endforeach ?>
</select>
<input name="submit" type="submit" value="go" />
</form>
I am trying to use HTML select dropdowns to help sort the search results on my website and I am having some trouble. In the code below, I specifically select Option1 and when I try to echo the result after I submit the form, it always echo's 'FAIL'. I was wondering if someone can help me understand what I am doing wrong and what is the proper way to retrieve the data from the option the user selected, after the form was submitted?
<?php
echo '<form method="post" action="search.php">
<select>
<option name="one">Option1 </option>
<option name="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
if(isset($_POST['one'])){
echo 'Option1 is set';
}else{
echo 'FAIL';
}
?>
thank you
This is because the name attribute goes with the select tag.
<select name="dropdown">
<option>Option 1</option>
<option>Option 1</option>
</select>
if(isset($_POST['dropdown'])) {
echo $_POST['dropdown']; //echoes 'Option 1'
}
If you like, you can add a value attribute to the option element, but you don't have to.
If you do
<option value="foobar">Option1</option>
The form will post foobar and not Option1.
<?php
echo '<form method="post" action="search.php">
<select name="my_option">
<option value="one">Option1 </option>
<option value="two">Option2</option>
</select>
<input type="text" name="searchword" />
<input type="submit" value="Search" />
</form>';
echo "My option value is : " . $_POST['my_option'];
?>
You need to name your select tag and access that instead.
<select name="options"></select>
echo $_POST['options']; will give you your selected option
Instead of name="one" an option needs a
value="myvalue"
and your select tag needs an
name="nameofthisthinghere"
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.