On a page I have form with the following inputs:
<input class="half" type="text" name="tourney" placeholder="BRDTNS16">
<select class="half" name="tourneydrop">
<option value="">--------</option>
<option value="BRDTNS16">Bordtennis</option>
<option value="SMSHBR16">Smash Bros</option>
<option value="MROKRT16">Mario Kart</option>
<option value="GEKOUT16">Geek out</option>
<option value="HRTSTN16">Hearthstone</option>
</select>
I want to override tourney if tourneydrop is set, I do this using the following code
if (isset($_POST['tourneydrop'])) {
$id = $_POST['tourneydrop'];
}
else {
$id = $_POST['tourney'];
}
But this is not working as intended and leaves the $id empty no matter what.
This is because, if tourneydrop is not set, then it should set $id to tourney, yet tourney has no value. Give it a value with
<input value="abc">
When tourneydrop is set, however, it can be set of a value of value="".
Related
I'm trying to store a $_SESSION variable on a form option for further use on the next page after submit (which will be reloaded and used multiple times after that, so a simple $_POST won't do)
the problem is that it doesn't matter what option I choose, it always saves the value from the last option ("Victoria" in this case) as if I picked that one.
session_start(); is already declared on top of the page
Am I missing something? Here is the form:
<form method="post">
<label for="turno">Turno:</label>
<select name="turno" id="turno">
<option value=DIA>Dia</option>
<option value=NOCHE>Noche</option>
</select>
<label for="planillera">Planillera:</label>
<select name="planillera" id="planillera">
<option value="<?php $_SESSION['planillera'] = 'Rosa'; ?>">Rosa</option>
<option value="<?php $_SESSION['planillera'] = 'Cristina'; ?>" >Cristina</option>
<option value="<?php $_SESSION['planillera'] = 'Maria'; ?>" >Maria</option>
<option value="<?php $_SESSION['planillera'] = 'Romina'; ?>">Romina</option>
<option value="<?php $_SESSION['planillera'] = 'Julieta'; ?>" >Julieta</option>
<option value="<?php $_SESSION['planillera'] = 'Victoria'; ?>" >Victoria</option>
</select>
<input type="submit" id='enviar' value="Registrar viaje" formaction="registrar_viaje.php">
</form>
That's not how $_SESSION or select works.
What you are doing is simply executing PHP where you set value of $_SESSION multiple times - hence why it always assigns the last value.
Your select should look like this:
<select name="planillera" id="planillera">
<option value="Rosa">Rosa</option>
<option value="Cristina">Cristina</option>
<option value="Maria">Maria</option>
<option value="Romina">Romina</option>
<option value="Julieta">Julieta</option>
<option value="Victoria">Victoria</option>
</select>
Then in the inside of registrar_viaje.php you would do:
$_SESSION['plankillera'] = $_POST['planillera'];
Lastly, you would do a redirect to another page.
I am creating a listbox box with 2 optional values Ja(Yes) or Nein(No). Additional iam using a default Value which is taken from the database for the record.
Here the code:
<td>
<select name="s_BKAG">
<option selected="selected" ><?php $bkagbruggwert = ''.$abc['BKAG (Brugg)']. ''; if ($bkagbruggwert==1){echo 'Ja';} else {echo 'Nein';};?></option>
<option value="1">Ja</option>
<option value="0">Nein</option>
</select>
</td>
Problem: Now i want that when Yes or No is selected, then id should take their value and if nothing is selected it should take the Value of the preselected default value. But the problem is the variable which i have created has the value "Ja", it does not matter what I'am changing on the formular. I do not know if I overseeing something.
The code of variable defining is here:
$BKAG_Brugg = $_POST['s_BKAG'];
I could take $BKAG_Brugg and define a new variable which i am asking, if the $BKAG_Brugg is "Ja" then set 1 to the new variable. But it's my first project on php and i do not really now how to do the query for the new variable.
Can someone help me with that? i would be thankful for every little help.
regards:
okanog
I think you can do like this:
<select name="s_BKAG">
<option></option>
<?php $bkagbruggwert = $abc['BKAG (Brugg)']; ?>
<option value="1" <?php if($bkagbruggwert == 1) { echo 'selected'; } ?>>Ja</option>
<option value="0" <?php if($bkagbruggwert == 0) { echo 'selected'; } ?>>Nein</option>
</select>
This way, if there is no preselected value, the empty option will be displayed; if there is a preselected value and this value is same as value of any option, it will be selected.
I hope this works for you.
You can do it this way,
Assign the default value to the hidden element
In case, if the user doesn't select one option, then assign the default value from the hidden element
function fetchValue()
{
var selectedValue = document.getElementById("s_BKAG").value;
if(selectedValue == "")
{
selectedValue = document.getElementById("default-value").value;
}
console.log("The selected value is: " + selectedValue);
}
<select id="s_BKAG" name="s_BKAG">
<option value="">None</option>
<option value="1">Ja</option>
<option value="0">Nein</option>
</select>
<input id="default-value" type="hidden" value="<?php echo $abc['BKAG (Brugg)'] ?>" />
<input type="button" value = "Fetch Value" onclick="fetchValue()" />
I am working on a registration form, to store family details in database table. I have my drop-down list of the number of brothers or sisters field.
<select id="purpose" required="required" name="purpose">
<option value="0">No Brother nor Sister</option>
<option value="1">Brother or/and Sister</option>
I would like to know on select of first option how to allocate null values for bro and sis fields in database.
The options with value 1 has got further additional fields that is is brother married or sister married. But if no bro nor sis exist then it should wrap at first stage only indicating bro and sis as null, followed by all further fields being null, like bro_married and sis_married.
My main form code :
<select id="purpose" required="required" name="purpose">
<option value="0">No Brother nor Sister</option>
<option value="1">Brother or/and Sister</option>
</select>
<div style='display:none;' id='business'>
<div>
<label for="username" class="uname">Number of Brothers</label>
<select id="bro" name="bro" required="required">
<option value="<?php if(isset($_GET['bro']))
{echo $_GET['bro'];}else{echo "";}?>"><?php if(isset($_GET['bro']))
{echo $_GET['bro'];}else{echo "Select";}?>
</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
My PHP file to save
if(isset($_POST['step_four_save']))
{
$bro=$_POST['bro'];
update_user_info=mysql_query("update partners_registration set bro='$bro' where email_username='$username' ");
Kindly advise, thanks.
I am not pretty sure why you need to check the null for your dropdown. Dropdown will have at least blank '' or some value of first child (option) if you do not select any value on form submit.
Here is the code if you are looking for them:
if("" == trim($_POST['bro'])){
//
}
Or
if (is_null($_POST['bro'])){
//
}
Assuming bro column in partners_registration is integer then following code can work
if(isset($_POST['step_four_save']))
{
if(isset($_POST['bro']) && $_POST['bro'] != "") {
$bro = $_POST['bro'];
} else {
$bro = NULL;
}
$update_user_info=mysql_query("update partners_registration set bro=$bro where email_username='$username' ");
}
I have a form that a user can insert his/her level of education either by picking a value from a drop down menu or just typing some text. My code is simple:
<label for="education">Education: <input type="text" name="education"/> </label>
<select name="education">
<option value=""></option>
<option value = "Primary education" name="Primary education">Primary education</option>
<option value = "Secondary education" name="Secondary education">Secondary education</option>
<option value = "Bachelor" name="Bachelor">Bachelor</option>
<option value = "Master" name="Master">Master </option>
<option value = "Doctoral" name="Doctoral">Doctoral </option>
</select>
So i want to insert that value in a column called education in database. Using code above the value is inserted only when someone is picking a value from menu. Input text is not stored in database.
In your textbox part;
Change
<label for="education">Hobby: <input type="text" name="education"/> </label>
to
<label for="education">Hobby: <input type="text" name="education_text"/> </label>
Duplicate name causes your problem(select and textbox name are same). I have given education_text as an example, update it according to your needs. Do not forget to handle it backend with updated name.
On your backend;
$education = "";
if(!empty($_REQUEST["education_text"])) {
$education = $_REQUEST["education_text"];
} else if(!empty($_REQUEST["education"])) {
$education = $_REQUEST["education"];
} else {
die("Invalid education");
}
And use it in your query. You can change $_REQUEST to $_POST or $_GET according to your needs
I have a question. Let me explain this with an example.
I have this piece of code:
<form action="vehicles.php" method="get">
<span>Marca:
<select name="brand">
<option value="null" selected="selected"></option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="null" selected="selected"></option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Is any way to prevent the assignment of those variables if the option selected is the "null" one?
If, for example, I select brand="Renault" and model="null" the url is
http://mywebpage.com/vehicles.php?brand=Renault&model=null
but it should be
http://mywebpage.com/vehicles.php?brand=Renault
I know how to unset the variables with "null" value after the form submission with PHP. Is any way to do it before the submission and after the variables are setted to "null".
I would like a cleaner url, free of "variable=null" results.
P/D: I don't have a native english speaking so feel free to edit my question. I wish you understand me.
I am afraid that you might need javascript to achieve what you want.
Take a look at this code:
<form action="vehicles.php" id="carForm" method="GET">
<span>Marca:
<select name="brand" id="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model" id="model">
<option value="none" selected="selected">-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="button" value="Submit" onclick="submitFormFilter();" />
</form>
Javascript : code will be like this:
<script>
function submitFormFilter(){
var myForm = document.getElementById("carForm");
var carBrand = document.getElementById("brand");
var carModel = document.getElementById("model");
if(carBrand.value === "none" || null){
carBrand.parentNode.removeChild(carBrand);
}
if(carModel.value === "none" || null){
carModel.parentNode.removeChild(carModel);
}
myForm.submit();
}
</script>
http://jsfiddle.net/7xzKP/1/
you can it try here.
Include the value attribute of your option tags.
<option value="" selected="selected">-</option>
You should really do this for all of your options.
if you don't want to use $_GET superglobal because of the url extension that it comes with try using $_POST instead. It will not have a url extension and it can still store values that you can later retrieve. Just be sure to change your method to equal POST instead of GET.
So the code for the form tag would change to:
<form action="vehicles.php" method="POST">
And you can later access it by (for example):
echo $_POST['brand'];
or
echo $_POST['model'];
as well, you probably want to add a value param to the values that you have in your option tag.
EDIT-
I've added this new section since you don't want to use POST even though I think you should.
You can stay with the GET method by doing this line of code:
<form action="vehicles.php" method="GET">
<span>Marca:
<select name="brand">
<option value="none" selected>-</option>
<option value="Volkswagen">Volkswagen</option>
<option value="Renault">Renault</option>
<option value="Peugeot">Peugeot</option>
<option value="Fiat">Fiat</option>
</select>
</span>
<span>Modelo:
<select name="model">
<option value="none" selected>-</option>
<option value="206">206</option>
<option value="Suran">Suran</option>
<option value="Passat">Passat</option>
<option value="Punto">Punto</option>
</select>
</span>
<input type="submit">
</form>
Let me know if that helps
You should set the value for the option tag of <select> :)
You's missing set value for option tag. you should set the value for option tag in select tag. With the blank value, you can assign it to a special value like zero(0)
<select name="model">
<option value="0" selected>-</option>
<option value="1">206</option>
<option value="2">Suran</option>
<option value="3">Passat</option>
<option value="4">Punto</option>
</select>
EDIT:
Ok. I got your point. You can import jquery to your page and make a check by javascript before you submit, if the value of select box is blank. you can remove it.
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$.ready(function(){
$("form").submit(function(){
if($("select[name='model']").val() == "0") $("select[name='model']").remove();
return true;
});
})
</script>