Simple if else php form script for select menu - php

Can you help me on creating a simple php script using select menu with if and else statement?
I'm trying to create select menu that when i click the menu, it will redirect to another page.
Im really dumb in programming. I will be very grateful with your help. I'm hoping my statement is clear. Thanks in advance...
<td width="421">
<label>
<select name="journal" id="journal">
<option value="IN PROGRESS">IN PROGRESS</option>
<option value="PUBLISHED"></option>
<option value="NOT APPLICABLE">NOT APPLICABLE</option>
</select>
</label>
</td>

You cannot use php for this unless you use a form to submit the select value to a php script, use javascript or jquery to achieve what you want, here's a jquery solution:
jQuery(function () {
$("#journal").change(function () {
selectValue = $(this).val();
if(selectValue == "IN PROGRESS"){
location.href = "http://somesite.com";
}else if( selectValue == "PUBLISHED"){
location.href = "http://othersite.com";
}else if( selectValue == "NOT APPLICABLE"){
location.href = "http://anothersite.com";
}
})
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td width="421">
<label>
<select name="journal" id="journal">
<option value="">Choose Status</option>
<option value="IN PROGRESS">IN PROGRESS</option>
<option value="PUBLISHED">PUBLISHED</option>
<option value="NOT APPLICABLE">NOT APPLICABLE</option>
</select>
</label>
</td>

Related

Creating dynamic button with Jquery

Hey guys I'm trying to create a dynamic button from a drop down list. for example, in the HTML code below...
function dropdownbutton(){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
alert("answer")
}
<div class="make">
<label>Make</label>
<select name="make" id="makelist" onchange="getId(this.value);">
<option value="">Select Make</option>
<option value="">1</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" onchange="getId2(this.value);">
<option value="">Select Model</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" onchange="getId3(this.value);">
<option value="">Select Year</option>
<option value="">1</option>
<option value="">2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
I'm open to trying it in different languages but I would prefer to do it in php, I simply don't know how to get the values from each dropdown.
I have taken help of Jquery here, inside the dropdownbutton() function i call all the select box element.
And using .each function extracted the value selected value of dropdowns.
i check those value if not empty then created a button inside element having Id #append_new_button
Please check the below code it might solve your issue
Thanks
function dropdownbutton() {
jQuery('#append_new_button').html('');
jQuery('select').each(function(){
var select_value = jQuery(this).val();
var select_label = jQuery("#"+jQuery(this).attr('id')+" option[value='"+select_value+"']"). text();
if(select_value != ''){
jQuery('#append_new_button').append('<input type="button" id="'+select_value+'" value="'+select_label+'"></button>')
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="make">
<label>Make</label>
<select name="make" id="makelist">
<option value="">Select Make</option>
<option value="make_1">Make 1</option>
<option value="make_2">Make 2</option>
</select>
</div>
<div id="model">
<label>Model</label>
<select name="model" id="modellist" >
<option value="">Select Model</option>
<option value="model_1">Model 1</option>
<option value="model_2">Model 2</option>
</select>
</div>
<div id="year">
<label>Year</label>
<select name="year" id="yearlist" >
<option value="">Select Year</option>
<option value="year_1">year 1</option>
<option value="year_2">year 2</option>
</select>
</div>
<button id="dropdownbutton" onclick="dropdownbutton()" class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-rounded vc_btn3-style-3d vc_btn3-color-success">Dropdown</button>
<div id="append_new_button">
</div>
I updated that code to find the values for each drop down
function dropdownbutton(val){
var make = document.getElementById("makelist");
var answer = make.options[make.selectedIndex].value;
var model = document.getElementById("modellist");
var answer2 = model.options[model.selectedIndex].value;
var year = document.getElementById("yearlist");
var answer3 = year.options[year.selectedIndex].value;
alert(answer2);
}
Now i need to figure out how to pass these variables to a link for example, mydomain.com/answer/answer2/answer3
i think that this code is more dynamic.
$("button").click(function() { // if button is clicked
var arr = $('select').map(function() { // run through all selects and get value
return this.value
}).get();
var url = "http://exp.com"; // base for the url
arr.forEach(function(item) {
url = url + '/' + item; // append all options to the base url
});
window.location.href(url); // redirect to base url + all options
});
Don't forget to add value to your options. <option value="1">1</option>
Please look at jsfiddle for a working example

how to redirect to new page by selecting an option in datalist

I have created a datalist here how can I add a link to option so that whenever i select a option and click on submit i should redirect to another page based on selected option.I have googled a lot but I couldn't any information about this.Thanks in advance
<input list="category" name="category">
<datalist id="category">
<option value="fruits">
<option value="animals">
<option value="vechiles">
<option value="ornaments">
</datalist>
<input type="submit">
You can try below code :
<input list="category" name="category" id="textcat">
<datalist id="category">
<option id="www.google.com" value="fruits">
<option id="www.fb.com" value="animals">
<option id="www.ymail.com" value="vechiles">
<option id="www.msn.com" value="ornaments">
</datalist>
<input id="btn" type="button" value="submit">
<script>
$('#btn').click(function(){
var textval = $('#textcat').val();
$('#category option').each(function(){
if($(this).val() == textval){
alert($(this).attr('id'));
window.location = $(this).attr('id');
}
});
});
</script>
Make some formHandler.php in which you will some routing, and point your form (action="formHandler.php") that includes this select
$redirectLink=$_REQUEST['category'];
header('Location:'.$yourmainSitePath.'/'.$redirectLink)
Are you satisfied ? :D

Redirect with HTML dropdown select

I have a simple HTML drop-down select box.
I would like this to function as 'once something is selected' > the page then redirects to a custom URL.
Below is my mark-up.
<fieldset id="size"><legend>Product Options</legend>
<div class="wpsc_variation_forms">
<table>
<tr><td class="col1"><label for="variation_select_48_5">Choose Size:</label></td>
<td class="col2"><select class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select></td></tr>
</table>
</div><!--close wpsc_variation_forms-->
</fieldset>
I found a similar question; but the solution given seems a bit clunky.
SELECT Dropdown that redirects without Javascript
With jQuery:
$(function() {
$('#variation_select_48_5').change(function() {
document.location = 'http://customurl.abc';
});
});
The easiest way i know of is assigning an onchange event to select element and making options' values as urls.
<select onchange="window.location = this.options[this.selectedIndex].value;">
<option value="http://your-url">Large</option>
</select>
I would write i litte function and then trigger it depending on the retuned result
<?PHP
function redirect($where){
header("Location: $where");
}
if ($_REQUEST['select1'] == '8'){
redirect('http://example.com/somewhere.php');
}elseif($_REQUEST['select1'] == '7'){
redirect('http://example.com/elsewhere.php');
}elseif($_REQUEST['select1'] == '6'){
redirect('http://example.com/elsewhere.php');
}
?>
<form>
<select name="select1" class="wpsc_select_variation" name="variation[5]" id="variation_select_48_5" onchange="this.form.submit()">
<option value="0" >-- Please Select --</option>
<option value="8" >Large</option>
<option value="7" >Medium</option>
<option value="6" >Small</option>
</select>
the only javascript required is within the select tag to trigger the submit onchange="this.form.submit()"
Hope this helps
$("select#wpsc_select_variation").change(function(){
if ($(this).val() !== 0) {
switch($(this).val())
{
case(8):
window.location.href = http://www.8.com
case(7):
window.location.href = http://www.7.com
case(6):
window.location.href = http://www.6.com
}
}
});

Dynamically generate a single new drop down menu in a html form

I have a html form defined . I have a button for the user to select his occupation . If his occupation happens to be a student then i need to generate another new button dynamically . If the user selects anything other than a student then i dont want any new button . I am unsure how to do . Do i use jquery ? I have no idea to use jquery . Can some one help ? Any pointers
Thanks.
<?php
?>
<script language="javascript" type="text/javascript">
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
</script>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<form>
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
</form>
</body>
</html>
Personally I accomplish this by placing the additional form element in the HTML, simply hidden like so:
<select name="occupation" id="occupation">
<option value="">- Select Occupation -</option>
<option value="Worker">Worker</option>
<option value="Student">Student</option>
</select>
<select name="school" id="school" style="display: none;">
<option value="">Select School Type</option>
<option value="4 Year">4 Year</option>
<option value="2 Year">2 Year</option>
</select>
Then you can use JS, as I prefer jQuery, to show the div or hide the div when applicable:
$(document).ready(function()
{
$('#occupation').change(function()
{
if($(this).val() == 'Student')
{
$('#school').show();
} else {
$('#school').hide();
}
});
});
Using jQuery would simplify this:
Let's say you have the following:
<form>
<select name="occupation">
<option value="Non-Student">Non-Student</option>
<option value="Student">Student</option>
</select>
</form>
Then the following would be used to append the button if the user selected 'Student':
$(function(){
$('select[name="occupation"]').change(function(){
var val = $(this).val();
if(val=='Student'){
$('form').append('<button>New Button</button>');
}
});
});

JQuery Mobile Change Attribute Each Option on Multiple Select Using JavaScript

I'm new with this JQuery Mobile thing. And the first problem i met in this is i can't set "selected" attr on multiple select option to "false" / unselected in JQuery mobile using Javascript.
A documentation i found told me to refreshing a select so i can manipulate it via javascript here in the bottom of it's page:
http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-selects.html
I've done it. But i don't know if i did it wrong or what. Here is my code :
<script type="text/javascript">
function SelectAll(){
var select=document.getElementById('sfruit');
if (select.options[1].selected == true){
alert('All Selected');
for (x in select.options){
if(x > 1){
if (select.options[x].selected == true){
alert(select.options[x].value+' selected');
RefreshSelect();
select.options[x].selected == false;
}else{
alert(select.options[x].value+' unselected');
}
}
}
}
}
function RefreshSelect(){
var myselect = $("select#sfruit");
myselect[0].selectedIndex = 5;
myselect.selectmenu("refresh");
}
</script>
<select onChange="SelectAll()" data-native-menu="false" id="sfruit" name="sfruit" multiple>
<option>Select Fruit</option>
<option value="all" selected>All</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
<option value="melon">Melon</option>
</select>
What i actually want is when i select "All" option, other option that have been selected become unselected.
I'll attaching the image if u all still doesn't understand what i mean, later, it's already late here.
Thanks guys. And please help me. .. :)
I think this is what you want
$(window).load(function(){
$("select#sfruit").change(function () {
$("select#sfruit option:selected").each(function (obj) {
if($(this).index()==1){
alert('All Selected');
$("select#sfruit option:selected").removeAttr("selected");
$("select#sfruit option:eq(1)").attr("selected","");
}
});
});
});
<select data-native-menu="false" id="sfruit" name="sfruit" multiple>
<option>Select Fruit</option>
<option value="all" selected>All</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
<option value="melon">Melon</option>
</select>

Categories