Query Select Sql with Condition from a Value of Html combobox - php

I have a problem of my web programming using php. I have an input form where there are two combobox with several options.
In the form, there also a textfield of Code where the value is determined by what is selected from both combobox (as the condition) from mySql database using select query.
Like= Select code from tableA where condition1=itemComboA and condition2=itemComboB;
I wanna ask, how to retrieve the value of the textfield automatically when user select an item in both combobox?
The query above still not working, then I manage it to select the code in another page then I redirect it to the input form by using link with the Code Like this: http://localhost/data/input.php?code=6
I still can't Set the value='<?php echo $code ?>;' in the value of textfield because the select query isn't working.

Your html will look like this
<form action="input.php" action="GET">
<select name="code">
<option value="1"> option 1</option>
<option value="2"> option 2</option>
<option value="3"> option 3</option>
</select>
<select name="code2">
<option value="drp1"> option 1</option>
<option value="dpr2"> option 2</option>
<option value="drp3"> option 3</option>
</select>
<input type="submit" value="Submit" />
</form>
for testing purposes, in your php file, print this to show content of your array
print_r($_GET);
from there you can get the values to put into your mysql query
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_GET['code1']) . ' AND condition2 = ' mysql_real_escape_string($_GET['code2']) . ';';

At the top of your php file you can have:
<?php
if(!empty($_POST) {
$query = 'SELECT code FROM tableA WHERE condition1 = ' . mysql_real_escape_string($_POST['itemComboA']) . ' AND condition2 = ' mysql_real_escape_string($_POST['itemComboB']) . ';';
}
?>
It seems as though your form doesn't have method="post" defined so you may need use $_GET instead of $_POST.

Related

Sql and php if not set

I have a scrolling list with different names which i can choose from but if nothing is selected want all of them to be selected.
html:
<form action="" method="POST">
<select name="names">
<option selected disabled value="">Choose name</option>
<option value="name1">Name 1</option>
<option value="name2">Name 2</option>
</select>
<button type="submit" name="submit">OK</button>
</form>
php:
if(!isset($_POST['names'])) {
*what to put here if i want all if not set*
}
Sql:
WHERE name = '"($_POST['names'].'"
Put the WHERE clause in a variable. If there are no names, make it empty.
if (isset($_POST['names'])) {
$where = "WHERE name = '" . $_POST['names'] . "'";
} else {
$where = "";
}
$sql = "SELECT * FROM yourTable " . $where;
However, when a form has a <select> element, its value will always be sent in the POST, so isset() will always be true. Perhaps you should be checking if (!empty($_POST['names'])); if you have an empty default option, this will test if that option is selected.

As I get the value of an option of a select ? symfony2

I have something like this in my view:
<form .....>
<select>
<option value="five">5</option>
<option value="ten">10</option>
<option value="fifteen">15</option>
</select>
</form>
As I do in the controller to take the values ​​of the options?
PD: It is a manual select , do not use types that actually genre with jquery .
You need to give your select a name, so it will be submitted in the form.
<select name="whatever">
Then in PHP, it will be in
$_POST['whatever'];

Why Can't I Select Dynamically (PHP) Generated Options?

I am having an issue with a table where each cell contains an id'd input or select field. The cell contents are dynamically generated from a MySQL query. Everything works fine except for my dynamically generated SELECT drop downs. I am using the following code in a switch statement:
case 'crew_1':
case 'crew_2':
echo '<td><select class="'.classExt($types[$key]).' '. $key .'_c" id="' . $key . '--' . $idx .'">';
echo '<option value="" selected="selected"></option>';
while ($rowtech = mysqli_fetch_assoc($rtech)){
echo '<option value="' . $rowtech['name'] . '">' . $rowtech['name'] . '</option>';
} mysqli_data_seek($rtech,0);
echo '<option value="TEST">TEST</option>';
echo '</select></td>'; break;
This creates the following HTML:
<td>
<select class="varchar crew_1_c" id="crew_1--1">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
</td>
From this I can select the blank or 'TEST' option and it returns the correct value in a console.log, but if I choose one of the dynamically generated names, the box returns back to the default blank value. I get the same responses on Chrome and IE.
Where is my disconnect?
Included for the person requesting the script generating to output to console:
$('tbody select, tbody input').change(function(){
console.log($(this).val());
});
OP, you're missing the name attribute.
This works fine for me:
<form method="post" action="">
<select class="varchar crew_1_c" id="crew_1:1" name="choice">
<option value="" selected="selected"></option>
<option value="Name 1">Name 1</option>
<option value="Name 2">Name 2</option>
<option value="TEST">TEST</option>
</select>
<input type="submit" value="Submit" name="submit" />
</form>
<?php
var_dump($_REQUEST);
?>
Found my issue. The JS request earlier had me dig back through my code to discover that I was setting my values toUpperCase() for submission for consistency, but that was breaking my value match on the drop down. I set my values to uppercase in the select options also and TA-DA!
Thanks for the help, guys. Sometimes it takes a fresh pair of eyes to point out the obvious.

How can I retain a option from a select field?

I have a <select>:
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
</select>
The user selects an option, then he press a send button to make a query, thru PHP. The result of the query appears in the same page, so the page reloads.
How do i retain the selected option? when the page reloads??
I mean, if they select spain, how can I see spain again when the page reloads?
You need to first give your select dropdown a name.
For example:
<select id="countries" name="countries">
Then you will have access to the value in your PHP when the form is submitted. The value can be retrieved like this in PHP (after submit):
$countries = $_POST['countries'];
Then you can do something like #JohnConde did by setting the selected attribute with PHP.
Here's a very basic way to do it:
<option value="1"<?php if (1 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>USA</option>
<option value="2"<?php if (2 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>Spain</option>

Two drop down lists in Ajax and MySQL

I'm trying to learn some Ajax and MySQL...
Basically, the code that I have right now is similar to: http://www.w3schools.com/PHP/php_ajax_database.asp
I was wondering how should I modify those codes (both html, javascript and php) to create two drop down lists. The first drop list would be used for choosing the Lastname (eg. Swanson) and the second drop list would be used for choosing the Hometown (eg. Quahog). Then the user would click "Submit" button and the query would return the matching results (eg. all the Swansons living in Quahog).
I'd be very thankful for all the ideas!
The only major difference to the html page will be the addition of another dropdown, addition of the submit button, and removal of the onchange event (assuming you want this removed since you're relying on the submit button). So instead of:
<form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
You now have:
<form>
Select a User:
<select name="lastname" id="lastname">
<option value="Griffin">Griffin</option>
<option value="Quagmire">Quagmire</option>
<option value="Swanson">Swanson</option>
</select>
<br>
Select a Hometown:
<select name="hometown" id="hometown">
<option value="Quahog">Quahog</option>
<option value="Newport">Newport</option>
</select>
<input type="submit" value="Submit" onclick="showUser(Document.getElementById('lastname').value, Document.getElementById('hometown'))">
</form>
The only change you need to the javascript is to change the showUser function to take and process the two inputs. So the declaration becomes:
function showUser(lastname, hometown)
and instead of the line:
url=url+"?q="+str;
you need:
url=url+"?lname="+lastname+"&town="+hometown;
Then in the PHP, you need only change the variable assignments and the query that is being executed. So:
$q=$_GET["q"];
Becomes:
$lname=$_GET["lname"]; $town=$_GET["town"];
And:
$sql="SELECT * FROM user WHERE id = '".$q."'";
Becomes:
$sql="SELECT * FROM user WHERE lastname = '".$lname."' AND hometown = '".$town."'";

Categories