extract dropdown boxe's value in php - php

I have small problem here. I have dropdown box in my form with 2 values; i.e Active and Inactive with Active being selected by default. I want to extract the selected value from the drop down box in PHP. I,m new to PHP and dont know how to do the same. Pls help. Thanks in advance

<select name="your_select">
<option value="active" selected">Active</option>
<option value="inactive"">Inactive</option>
</select>
...
<?php
$isActive = !empty($_POST['your_select']) ? $_POST['your_select'] : null;
?>
Note: The sample above is assuming your form method is post.

When the form is submitted by the user, to your PHP script, you'll find the submitted data in either $_GET or $_POST -- depending on the method of the form.
Using the var_dump() function, you'll be able to see what's in those :
var_dump($_GET);
or
var_dump($_POST);
will show you what's inside those variables ;-)

Related

PHP: Get the value from javascript using getElementbyId

I would take the value of the combobox by using getelementbyID to PHP variable,
And The Code Like This
<select name="PROPINSI" onchange="document.getElementById('KOTAA').value = this.value" >
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="KOTA" id="KOTAA">
<?php
$propinsi = ; //in this variable, I want take this value
$sqll="SELECT kota FROM master_propinsi WHERE propinsi = '$propinsi'";
$hasill= mysql_query($sqll);
while($dataa = mysql_fetch_array($hasill))
{
echo "<option value='$dataa[kota]'>$dataa[kota]</option>";
}
?>
</select>
Can SomeOne Help Me?
I really appreciate your answer
Thanks
How to get value From Combo Box with ID="KOTAA" to variable $propinsi
You cannot pass variable values from the current page javascript to the current page PHP code... PHP code runs at the server side and it doesn't know anything about what is going on on the client side.
You need to pass variables to PHP code from html-form using another mechanism, such as submitting form on GET or POST methods.
You can't put javascript code in PHP. Although, there is an alternative: Ajax

Create <select> elements on the fly and send multiple POST as an array using jQuery and PHP

I wonder if it's possible to populate <select> elements with option values generated by PHPto a page (after load) whenever a user is clicking a button withid='add'and then be able to store each value selected as an array then send this via $_POST to a PHP page?
I would like to populate the <select> element like this:
/theloader.php
echo "<select class='plan_id'>\n";
while ($row = mysql_fetch_array($course_elements)) {
echo "<option value='".$row['plan_id']."'>".$row['plan_name']."</option>\n";
}
echo "</select>\n";
?>
This will output
<select class='plan_id'>
<option value='1'>Description</option>
<option value='2'>Description</option>
<option value='3'>Description</option>
</select>
Then when the user has populated the needed elements and selected values, click save and send everything to anotherfile.php:
$("#save").click(function() {
// Store each value as a variable then send to /anotherfile.php
});
I lack knowledge in how to perform this kind of action and would really appriciate som tips in the right direction.
Thank's in advance.
Is there any reason the population of the selects has to be dynamic? Does the data change, or are you just letting the user add more line items to choose from pre-set options?
If the data in the dropdown itself is dynamic, you will need a combination of javascript and ajax calls to get the data from the php in real time.
If the options are not dynamic, then you do not need ajax at all, just populate the variables in javascript and create an action to add more dropdowns using the already defined variables.

How to add a modify search function to a results page

I have a PHP page which displays the results of a users database query for a classified ad's site. The user initiates a search on the homepage and selects the parameters of the search using select boxes and then the form is submitted.
Once on the results page I need to allow the user to modify the search parameters using select boxes down the side of the page (the same ones on the homepage). How do I go about making the option selected in each select box match what was selected on the first page. i.e. if the user selects make: Audi and Model: A8 I want the drop down for make and model to list all the other options, but have the Make preselected to Audi and the Model pre-selected to A8.
I know about using option selected for option boxes in a select statement, but how can I take a select statement that has already been coded for the results page and insert a selected variable for the option that should be selected first.
I hope that makes sense. I haven't posted any code because I'm in search of ideas rather than help fixing a code problem.
When the user submits the search form, all the variables posted in the form is available to you via $_POST or $_GET. Use these variables to re-select the correct information in the search form on the results page.
<select name="model">
<option value="a8" <?php echo (isset($_POST['model']) && $_POST['model'] == 'a8') ? 'selected' : ''; ?>>A8</option>
</select>
EDIT
The dynamic database way:
<select name="model">
<?php
$getRows = mysql_query("SELECT * FROM models");
while($row = mysql_fetch_assoc($getRows))
{
echo '<option value="" ' . ((isset($_GET['model']) && $_GET['model'] == $row['model']) ? 'selected' : '') . '></option>';
}
?>
</select>
Now you only need one if statement. You should btw use GET parameters, so that the user has the opportunity to bookmark the results for later use.

Autofill form when link is pressed

I have this webpage. It has a page called "services.php". I have several buttons (made of classes), that belong to different "package" prices i offer.
I want the links that say "Select" to autofill a form in another page, or alternativly in a popup form in the page..
I don't really know how to explain it, but as short as possible:
When link is pressed autofill form (in this or other page) with the type of package they chose. Only text autofill
What you seem to be asking is 'loading' a page pre-filled with specific information, you can do this a number of ways, either by utilizing javascript (like jQuery for instance). Or using your PHP, make links that pass variables (say a flag or a reference to pre-fill the fields -- if you want a popup or next page, etc).
Your url would like like the following for the button that a user presses (button would be a simple http link):
http://mywebsite.com/prefill.php?user=bob&package=2
This would have the values bob as the user that requests it (you can reference an id for user info here as well), and package=2 to designate your package options.
Then on the prefill.php page, you would have something that checks for:
$user = $_GET['user'];
$package = $_GET['package'];
Hope that helps
This will populate form fields with whatever you pass to the autoFill() function. This would be a same page example.
<html>
<body>
<form>
<input type="text" id="packageDescription">
<input type="text" id="packagePrice">
</form>
<script>
function autoFill(packageDescription, packagePrice) {
document.getElementById('packageDescription').value = packageDescription;
document.getElementById('packagePrice').value = packagePrice;
}
</script>
Premium Package<br>
Platinum Package
</body>
</html>
You could do something like this:
<select id="packages">
<option value="package1">Package 1</option>
<option value="package2">Package 2</option>
</select>
Submit
When the link is clicked, the following javascript will fire off:
function submitPackage()
{
var package = $("#package").val();
window.open("http://your-site.com/some-script.php?package=" + package);
}
The above will open a pop up window to a page such as this:
http://your-site.com/some-script.php?package=package1
In some-script.php you will do something like this:
You selected the package: <b><?php echo $_GET['package'];?></b>.
Or:
<?php
//Put the packages in an array:
$packages = array();
$packages['package1'] = 'Package 1';
$packages['package2'] = 'Package 2';
//...
?>
<select id="package">
<?php foreach ($packages as $name => $text):?>
<? $selected = ($name == $_GET['package']) ? 'selected' : '';?>
<option value="<? php echo $name;?>" <?php echo $selected;?>>
<?php echo $text;?>
</option>
<? endforeach;?>
</select>
The above will auto select the package they selected in a dropdown box.
if i understood your problem, you want to fill some input fields with information when the user clicks on some links
i can think of 2 ways of doing this : either have the links point to a page like services.php?req=package1 (or any other url you want) and on that page generate the input fields with the information you need (set the default values in the fields with the ones you want), or, use javascript to change the values of the forms without changing the actual page (either via ajax or predefined values)
for javascript you can use the jQuery framework, it has a pretty extensive community of enthusiasts and plenty of examples to get you started with it.
an example for your case would be
$('#btn1').bind('click', function() {
$('#input1').val("value");
$('#input2').val("value2");
});
replace btn1 with the id of the first button or link you have, input1 with the id of the first input in your form, and value with the value you want
I just did this myself. My solution was with jQuery. Just assign an id to your link. The first ID in the code is the link id and the second is the id for the input element you want to populate.
Here is the script:
<script>
$(document).ready(function() {
$('#link_id').click(function() {
$('#input_id').val( $(this).text() ).keyup();
return false;
});
});
</script>
Hope it works!
I've ran several time into the same issue, so I had to write my own script doing this. It's called Autofiller and its pretty simple but does great job.
Here is an example
http://example.com/?autofiller=1&af=1&pof=package&package=package1
So basically it takes several parameters to init the script:
autofiller=1 - init AutoFiller
af=1 - Autofill after page is loaded
pof=package - Find the parent form element of the select with name attribute package. Works also with input form elements.
package=package1 - Will set the select element's value to package1
Hope it helps you! :)

how to call to controller function when user selected a value in drop down list?

how can i create a drop down list with action which allow me link to other page?
i using cakephp to doing a system, which i wanna have a drop down list to let user select.
when user selected the value, i wanted to call a function in controller..
izit available that i can using a $form->input to perform this??
how can i call a function in a controller in this situation?
can i call a function while user selected a data and i send the data to controller??
any 1 can help? thanks..
As mentioned you will need to do this via javascript. I will give you a quick example and hopefully it will help. The key is the form that the select box is in, that form's action and other inputs will help direct your script. For example:
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
<select name="selectbox" size=1 onchange="this.form.submit();">
<option value="">Please choose your page</option>
<option value="1">Page option 1</option>
<option value="2">Page Option 2</option>
</select>
</form>
In the example above whenever the select box is changed it will call the form submit action, which will submit the form via post to the action, in this case itself. Once the page reloads to your PHP script you can look at the $_POST variable and see which page they choose and then redirect them.
switch( $_POST['selectbox'] ) {
case 1:
//Redirect or include page 1
break;
case 2:
//Redirect or include page 2
break;
}
I hope that helps.
This is solvable on the client with Javascript. PHP isn't directly useful for that, nor is CSS.
You're thinking about the select element

Categories