How to get VALUE from FORM without Submitting it? - php

I would like to get value from Form without submitting it, because the client have to choose the right type of house model to get the right form that fits the selected house model, without submitting, just selecting the house model and it for example continues the rest of form after that.
I have so far tried with this:
<form method="GET" action="foo.php">
<select name="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
<?php
$a = $_GET["housemodel"];
if($a<>'')
{
if($a == $model1)
{
echo "<input type=\"text\" name=\"a\" value=\"something model1\">";
}
else if($a == $model2)
{
echo "<input type=\"text\" name=\"b\" value=\"something model2\">";
}
else if($a == $model3)
{
echo "<input type=\"text\" name=\"c\" value=\"something model3\">";
}
}
?>

I think, if you dont want page to be refreshed when user selects value from your drop down list, then you can use ajax. AJAX is used for this purpose. if you google, then you will find lots of tutorials related to AJAX.

If you don't want to submit the form, use JavaScript to get the element. Or you can also use jquery to access the value. Make sure you put id = "something" and retrieve it using $('#something').val();
if you want to use JavaScript,
<select name="house_model" id="model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
<script type="text/javascript">
var model= document.getElementById('model');
alert("You entered: " + model);
</script>
AJAX codes here
$("#model").live("change", function () {
alert("You choose " + $('#model').val());
});

IF you don't want to submit your form, you can get element using AJAX/jQuery.
<form method="GET" action="foo.php" onChange="getHouseModel">
<select name="house_model" id="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
<script type='text/javascript'>
function getHouseModel(){
var model=$('#house_model').val();
alert(model);
}
</script>
or you can write jquery like this. so you dont have to call function in tag
<script type="text/javascript">
$('#house_model').select(function() {
var model=$('#house_model').val();
alert(model);
});
</script>

There is no way of getting data from a form before submitting in in PHP! This is because before submitting the form the PHP script simply is not called.
If you want to do some validation before sending data you must use javascript which is run on the client browser. Have a look at the jQuery library which makes this very easy: http://docs.jquery.com/Plugins/Validation/validate
If you want to use PHP only then you will need to separate the form into two pages.

You have to use ajax for achiving this.For example
<form method="GET" action="foo.php">
<select name="house_model" id="house_model">
<option value="">------</option>
<option value="<?php echo $model1;?>">Model 1</option>
<option value="<?php echo $model2;?>">Model 2</option>
<option value="<?php echo $model3;?>">Model 3</option>
</select>
</form>
$("#house_model").live("change", function () {
//Write the ajax and post the value to server side
});

Related

GET the value of the selected option using php then show it with echo

I'm Beginner on PHP, Javascript, I would like to get selected value from select/option and stock it into a php variable ( $varpeoplesnames ), to use it in a future SQL request and show it with echo
my code:
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<?php (....) echo $varpeoplesnames;
How can I get the text of the selected option to show it with echo in php?
Wrap it in a form with action being the link to your php file and a submit button
HTML:
<form action='yourPhpFile.php' method='GET'>
<select name="peoplesnames">
<option value="1">john</option>
<option value="2">sarah</option>
<option value="3">samantha</option>
</select>
<input type='submit'></input>
</form>
PHP
<?php
$varpeoplesnames = $_GET['peoplesnames'];
echo $varpeoplesnames;
?>

How to add a 'selected' in a select dropdown wp/php

I have a problem which I'm not sure how to approach, I have a WP plugin which has a form, in the form I have a with result that could have varied amount of data (depending on what the user submits).
How can I add 'selected' to the selected item so when the user returns he can edit/view the item he selected?
<select name="supplier">
<option value="Supplier 1">Supplier 1</option>
<option value="Supplier 2">Supplier 2</option>
<option value="Supplier 3">Supplier 3</option>
<option value="Supplier 4">Supplier 4</option>
</select>
A loop comes to mind, shall I assign a number to each option? There may be 100 suppliers so it would need to count right?
I would use a loop like...
$theirChosenSupplier = $supplierVarFromDatabaseOrWherever;
?>
<select name="supplier">
foreach($allSuppliers as $individualSupplier) {
if($individualSupplier == $theirChosenSupplier) {
$selected = "selected";
} else {
$selected = "";
}
?><option value="<?php echo $individualSupplier; ?>" <?php echo $selected; ?>>
<?php echo $individualSupplier; ?>
</option>
<?php } ?>
</select>
This code isn't tested but should give you an idea. Apologies if I've misunderstood the question.
adding an id is always a good thing. Assuming this is wordpress generated and you don't have a loop already there is always the option of using javascript and maybe an onchange event.
I dont know if the form is submitted first or you do some other stuff but maybe:
<select id="foo">
<option id="provider_1">Provider 1</option>
<option id="provider_2">Provider 2</option>
</select>
<script type="text/javascript">
window.onload = function() {
var selectedValue = '<?php echo someEscapeFunction($_SESSION['id_provider']); // or _GET, _POST ?>';
document.getElementById('foo').selectedIndex = document.getElementById(selectedValue).index;
}
</script>

Get the selected option of select & show the selected option in PHP

I have tried a lot & searched a lot but i didn't found the solution.
I have HTML code
HTML
<form id="program_mode" action="" method="post">
<select name="mode" id="mode">
<option>Select</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/all" <?php if($mode == 'All') echo 'selected="selected"'; ?>>All</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/anytime" <?php if($mode == 'Online') echo 'selected="selected"'; ?>>Online</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/offline" <?php if($mode == 'Offline') echo 'selected="selected"'; ?>>Offline</option>
</select>
</form>
Then i want in php part the selected option.
So i have written like
PHP
$mode = $_POST['mode'];
var_dump($mode);
But i am not getting the value. The thing i dont want to submit. Onchange only it should get the value. Can it will be possible ?
<form id="program_mode" action="" method="post">
<select name="mode" id="mode" onchange="val_changed()">
<option>Select</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/all" <?php if($mode == 'All') echo 'selected="selected"'; ?>>All</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/anytime" <?php if($mode == 'Online') echo 'selected="selected"'; ?>>Online</option>
<option value="<?php echo $vars['url']?>pg/event_calendar/offline" <?php if($mode == 'Offline') echo 'selected="selected"'; ?>>Offline</option>
</select>
</form>
With little jquery you can do with this
<script>
function val_changed()
{
var mode=$('#mode').val();
}
</script>
Ya sure, if you want to submit the form you can use form.submit() in onchange event of the dropdown like:
<select name="mode" id="mode" onchange="form.submit();">
And can access the value in PHP like:
if(isset($_POST['mode'])){
var_dump($_POST['mode']);
//extra code
}
If you dont want to submit the form you can just use a jquery function to pick the selected value: Then remove the onchange event handler from select box as:
<select name="mode" id="mode">
and just add the following jquery function:
$('#mode').change(function(e) {
var val = $(this).val();
alert(val);
});
If you want PHP to get the value only when the option is change, you should use JavaScript or jQuery.
Include the jQuery library :
<script src="http://code.jquery.com/jquery-latest.js"></script>
Then add the following code in a <script></script> tag.
$(document).ready(function(){
$("#mode").change(function(){
$.post("php_file.php", {mode:$(this).val()});
});
});
When the option changes, jQuery will send a POST request in the background to the php_file.php with mode value. PHP will take care of the rest.
This works normally like a form submitted to the PHP file. So you will get the $_POST['mode'] value in the PHP file.

I want the last selected option to be selected in my <select> form after the user selected the option

<form method="get" action="index.php">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select></form>
The code above displays a dropdown menu of options, such as a, b, or c, the user can click on. After the user clicks on a, b, or c from the drop down menu, that option is submitted in the form, and the page reloads with a new $_GET variable "choice" defined as a, b, or c. I want the option that the user last selected to now be the default selected option listed. For example, after the user clicks on "b" from the dropdown menu, "b" shows up as selected on the drop down menu list. What's the best way to accomplish this?
Here you go:
<?php
if (isset($_REQUEST['choice'])) {
$selected_choice = $_REQUEST['choice'];
}
else {
$selected_choice = "none";
}
?>
<html>
<head>
</head>
<body>
<form method="get" action="">
<select name="choice" size="1" onchange="this.form.submit()">
<option value="a" <?php if($selected_choice == "a"){ print "selected='selected'"; } ?> >a</option>
<option value="b" <?php if($selected_choice == "b"){ print "selected='selected'"; } ?> >b</option>
<option value="c" <?php if($selected_choice == "c"){ print "selected='selected'"; } ?> >C</option>
</select>
</form>
</body>
</html>
When outputting the form do a check like:
<option value="a"<?php if ($_GET['choice'] == "a") {?> selected <?php } ?>>a</option>
You'd then want to extract that into a function that either does the conditional code for you, or outputs the entire option element with the provided value, label, and sanitized data with the check done internally.
Another approach to avoid spaghetti code (or at least reduce it) is to set it through Javascript, like:
<html>
<body>
<form method="get" action="test.htm">
<select name="choice" id="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
</form>
<script>
document.getElementById('choice').value = 'b';//replace with something like: <?php echo $_GET['choice'] ?>
</script>
</body>
</html>
IMHO this approach is better than put a test in each option tag.
Try saving the posted select option in session like below
<?php $_SESSION['current_select'] = $_REQUEST['posted_select_option']?>
And in page load check as below
<script>
var currentSelect = <?php echo $_SESSION['current_select']; ?>
$(document).ready(function(){
if(currentSelect!="")
$("#choice").val(currentSelect);
});
</script>
Considering your select tag has id like below
<select name="choice" id="choice" size="1" onchange="this.form.submit()">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">C</option>
</select>

How can I use PHP to validate a drop down list

I want to validate a form using PHP. Below is the sample of my form:
<form>
<select name="select_box">
<option value="0">Please Select</option>
<option value="1">OPT 1</option>
<option value="2">OPT 2</option>
</select>
<input type="submit" value="Go!" />
</form>
<?php
if(isset($_REQUEST['select_box']) && $_REQUEST['select_box'] == '0') {
echo 'Please select a country.';
}
?>
In your php script where are you are submitting your form (in this case it's the same script that echoes out the form, since you aren't specifying action and method attribute to your <form>), you can get values of inputs by doing $_GET['name_of_input'], which in your case:
if(isset($_GET['select_box'])) { // do something with value of drop down
<?php
if ($_GET['select_box'] != '0') //form validation
{
//your code here
}
?>

Categories