Implement onclick retrieve - php

I am new to PHP and I need help.
I have a MySQL Database table named CARS. It has columns BRAND, MODEL, TYPE. I want to show 2 select combo box. First combo box should retrieve the BRANDS from Database. And when user selects a BRAND then MODELS of that BRAND should be retrieved in the second select combobox.
Please help.
<select id="brands">
<?php while($row1 = mysqli_fetch_array($result1)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[0];?></option>
<?php endwhile;?>
</select>

I'm not sure what you want, but I'll try to understand it.
So, first Combo box should get all brands from DB and second one needs to show its models.
What you need to do:
You need to call javascript on-click function that will trigger query from database.
Also, you have to send value of selected brand as parameter of function just because you want to make a query with that parameter.
I'm providing you with a helpful link:
https://www.daniweb.com/programming/web-development/threads/356822/onclick-get-results-from-php

Related

Insert Multiple Values > One Column from POST Form

I have a form with this select:
<select name="territory[]"><option vaue="None Selected">Please Select</option>
<option value="Argentina">Argentina</option>
<option value="Australia">Australia</option>
// And so on...
</select>
Next to the select option is a button where they can add another country with the same select options. Now when they submit I want them to go into one column in the database called COUNTRIES. So if a person selects 3 countries such as Argentina, Australia and New Zealand, they will display in the database column countries like:
COUNTRIES
------------------
Argentina, Australia, New Zealand
I have been looking and UNION has popped up but I am unsure how to use this as other fields are single entries.
INSERT into b_project (NAME,COUNTRIES, CLIENT)
VALUES ('$_POST[name]','$_POST[countries]','$_POST[client]')
I have done it before where I have inserted them onto new rows in the table using:
foreach ($_POST['name'] as $index => $id)
Then the insert statement with '".$_POST['countries'][$index]."'
But I need all the values in one column. Your advice would be much appreciated.
The simple (but a workaround) answer:
$countries = implode(',', $_POST['countries']);
This will glue the countries with a comma.
The way you should do it:
Use another table in your table, in which you provide all the countries.
Then when a user submits the form, you need another table which map the country_id with the b_project_id. For each country you will have a record in that table.
It little bit more work, but that's the way it should be done.
Last piece of advice:
Please sanitize the inputs. You do not know for sure what a user will send with the POST data. It can be everything...

how a website user interacts with a database?

I'm new to all this, and I can't seem to find the right search on google to get the answer I'm looking for. I have a front end that a user will use to create a new transaction. There is a drop down menu tied to a "categories" table that has a 1:M relationship with the "transaction" (see image). My question is, if a user submits this form to save a new transaction, they don't know the ID from the categories table, all they'll be doing is selecting one from a drop down that's pulling from the categories table. All I know how to do at this point is take their input from the drop down and store it into a field in the transaction table. I'm trying to understand how to use relationships, but in my limited understanding of how keys work, it seems that unless you knew what the ID number was, then the relationship does no good. If all you're doing is taking the submission from the drop down and storing it in the transaction table, then you've still just got the category name stored in 2 different tables, so what's the point? I'm missing a step in here somewhere and I'm hoping someone can help make sense out of this.
The user wouldn't know the Category ID. (Though they could if they wanted to. It's in the markup.) More importantly, your code would know the Category ID. When creating that drop down list (the select element), your code would be populating it with both the ID and the Name. The ID is used for the value, the Name is used for the display text.
The resulting markup might look like this:
<select name="category">
<option value="1">First Category</option>
<option value="2">Second Category</option>
<option value="3">Third Category</option>
</select>
And so on.
When the user selects one, they're choosing based on the text. But the actual form will post the value of the selected option back to the server. So if the user selects Second Category then in your server-side code $_POST["category"] will contain the value 2, which is the ID you need to insert into your Transaction record.

PHP/SQL - Using a Select element to display/link to rows in a database

I'm building a CRUD app with PHP, and I need some help trying to simplify the user experience. Basically, the user creates a new "Project" with a pre-assigned ID number. This ID number acts as the primary key. Upon submission of the form, a row is created in a "Projects" table in my database.
After the project is created, I want to use another form to add storage transactions to the project (for contextual purposes, I'm tracking warehouse storage). Initially, was going to have the user re-add the ID number (the primary key entered when the project was created), but there are lots of opportunities for human error if the ID number is entered incorrectly.
Current Task: I want to create a "Select" element on my form that lists all ID numbers that have been entered. Then, I can just select one of the projects from that drop down and have the transaction automatically applied. How can I do this?
Thank you in advance for your help!
First, you'll have to do a query (using PHP) to pull a list of all the project records in the project table from that database. When you do the query for that, you'll have a result set as an array of each project in the projects table that you can iterate through using a loop to populate a drop-down box.
First get the projects (assuming a column name of id that you're using for the drop-down selection):
<php $projects = mysqli_query($db, "SELECT id FROM PROJECTS_TABLE"); ?>
Then, to display the drop down selection box:
<form name="formWhatever">
...
<select name="project_id">
<?php foreach ($projects as $project) {
$result = mysqli_fetch_array($projects, MYSQLI_ASSOC)?>
<option value="<?php echo $result[id]; ?>">Project #<?php echo $result[id]; ?></option>
<?php } ?>
</select>
...
</form
I think I also like #pajaja's idea for using an index page to eliminate a long drop-down box as well.
Well all you need to do is what you described, so I'm not sure if i understood your question exactly. Create an select menu in the form and populate it with all projects. You need to get all project IDs and their names from the database first and generate HTML options for each project ID.
<select name="project_id">
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
<option value="4">Project 4</option>
...
<option value="n">Project n</option>
</select>
in PHP you will have something like this for each project
echo "<option value='{$project['project_id']}'>{$project['name']}</option>";
and then use the $_POST['project_id'] to get the project_id of project to which you want add storage transaction.
Hints:
Why are you preassigning project IDs? How are you assuring that you don't issue the same preassigned key twice? You can set the database column to be auto incremented on each insert. Also I think that it is better to first display a list of all project, like index page, and then when the user clicks on desired project you present him the option to add storage info (or whatever else you have planned). Having a general form and then selecting the project within will work but can get complicated if there are many projects, and i don't really see the advantage of that approach.

Output content of a form on the same page for public view

I'm trying to build a simple form with only dropdown option like
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
All I need is when the user selects one of the options, the option to be automatically submitted (without having to click submit) and posted on the same page for public display.
The selection of different users should be viewable as well and just appended to previous users' choices.
For example:
First user selects Volvo, then the page will display
Volvo
When second user selects Audi, the page will now append his result to the previous user's result
Audi, Volvo
Thus every visitor will see what the previous user chose.
And so forth. There should be a limit to how many choice are displayed by users, say 100, and beyond that the original choices will be truncated.
Thanks.
Try like this.. in database create table, and store there users choices.
And on every submit insert the choice into that table. When you show the page select from Your table with limitations ordering by inserted date desc, and filter Your data by that choices before printing or query.
You should be able to set the
onchange()
event in your select tag to a submission (directing to the same page) and then using passed parameters you can determine that the submission went through.

Populate a drop down box with values from database using php and sql

I want to have a drop down box that has a list of different sizes of item. I have a table called items in my database with an attribute inside it called sizes. This maps to a table called sizes which lists the different sizes with different prices etc.
I simply want to create a drop down box within the items web page which lists the sizes associated with that item. How can I use php to fetch the sizes and display them in a drop down box?
I have got items table which has a composite key of item_id and size_id which is mapped to sizes table which has a primary key of size_id.
I have tried to find information from the net but to no avail.
Thanks
You can use HTML's SELECT and its OPTION something like:
<select name="mySelect">
<?php $result= mysql_query('SELECT * FROM items'); ?>
<?php while($row= mysql_fetch_assoc($result)) { ?>
<option value="<?php echo htmlspecialchars($row['your_column_name']);?>">
<?php echo htmlspecialchars($row['your_column_name']); ?>
</option>
<?php } ?>
</select>
Of course, you can add a ORDER BY ... to the sql query above for sorting. Then depending on your form method, you can access this by using $_POST["mySelect"] or $_GET["mySelect"]
You can write two queries or you can use join to fetch values from both the tables at a time..
But for that I would like to know what type of mapping are you using between items table and sizes table..

Categories