Reputation: 2597
I have a little knowledge of php, but this is what I'm trying to do. I have a form
with select option
. If an option is selected with the value "example_5" I want it to get the value of an <input>
. Is this done with php or jquery to use the value of input?
For example: someone selects example_5. They can now write something in the <input>
. And that value will be used.
<script type="text/javascript">
$(function(){
if($('select').find('option:selected').val() == 'example_5'){
use input??
$_COOKIE['input_example'] = $example_1;
}
else{
$_COOKIE['example_1'] = $example_1;
}
});
</script>
<?php
$_COOKIE['example_1'] = $example_1;
?>
<form method="get" action="page2.php">
<select name="example1">
<option value="example_1">example_1</option>
<option value="example_2">example_2</option>
<option value="example_3">example_3</option>
<option value="example_4">example_4</option>
<option value="example_5">example_5</option>
</select>
<input name="input_example" type="text">
</form>
Upvotes: 0
Views: 2288
Reputation: 6850
The way I would do it is to have a onChange
listener on the select
box and then using javascript get the value of the input box using document.getElementById
. You can use any jQuery shortcut you want, but the methodology would be the same.
See this fiddle for a functional example using this technique.
On an unrelated note, be sure to always close your <input />
tags (probably just a typos :)
As per comment : just set the value of the input from a value from the cookies.
document.getElementById("input_example").value = getCookie("cookie_name");
Where you can write a simple function to get the cookies :
function getCookie(Name) {
var search = Name + “=”
if (document.cookie.length > 0) { // if there are any cookies
offset = document.cookie.indexOf(search)
if (offset != -1) { // if cookie exists
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(”;”, offset)
// set index of end of cookie value
if (end == -1)
end = document.cookie.length
return unescape(document.cookie.substring(offset, end))
}
}
}
Tutorial for js cookie function taken here.
Upvotes: 1
Reputation: 12591
Based on your comment to blo0p3r's answer.
In page2.php, add the following at the very top of the script before any whitespace or HTML is sent:
<?php
if (isset($_GET['example1']) && $_GET['example1'] === "example_5")
setcookie($_GET['input_example'], 'example_5', time()+3600, '/');
Upvotes: 0
Reputation: 17009
First, you want to check if the from is submitted. Afterward $_COOKIE['example_1'] = $example_1;
would be something like this:
$_COOKIE['example_1'] = $_GET['example1'];
Upvotes: 0