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...
Related
I have two table program(p_id(pk)AI, program_name and another table graduate_survey(id(pk)AI,total_PO1,total_PO2,session,p_id(fk). In the program table values are already inserted as
p_id Program_name
1 B.tech CSE
2 B.tech IT.
..so on. I select from a drop down list B.tech CSE and then redirect to the survey form. I enter the total_PO1, total_PO2 there and submit it.Now i want to insert in the graduate_survey table where in the fk field the p_id of B.tech CSE should automatically enter so that i can know the survey is done for which program..Is there any query in MySQL to do that? The insert operation should be done in php code.Please suggest any query.
Your approach seems to be incorrect. When you generate your dropdown menu in HTML, then the display value (the value that the user actually sees and selects) should be the program name, but the underlying value should be the id of the same record.
When you send the data to the server, then you should send the underlying id value, not the display value. Using HTML's standard dropdown menu, you would create sg like this as an output:
<select name="programs">
<option value="1">B.tech CSE</option>
<option value="2">B.tech IT</option>
</select>
If this control is submitted via a form, then the browser will send the value of the selected option automatically. If you use ajax, then your code has to retrieve the selected value of the select element. In the php code you just insert the underlying value into the graduate survey table.
I have made a webpage for a school project where I have a series of selection boxes (some are independent and some are depended on another) to make selections and then apply filter to make a query. It is still in test-mode and working fine for single selections. You can find the webpage here: http://gorevler.awardspace.biz/realdeal03.html
For example, when I select Europe from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter it gives out the result.
But, when I select Europe AND North America from Region, Austria from Country, Plastics from Sector, Plastic Raw Materials from Sub-Sector and Polybutylene from Product, and click apply filter, it gives only one result. but there are two records matching this filter in the database. So it does not recognise my multiple selection. Any advice about how I could make it work so that when I make multiple selection on a box and apply filter, it gives all the results including all the selection?
I guess it is something about the PHP coding, and I tried different things but no success yet. Since I am not a programmer, I don't seem to understand where the problem lays. Any advice about the coding would be appreciated.
Is it because when you select two regions your query becomes:
SELECT ... FROM ... WHERE region ='North America' AND region = 'Europe' which means you will never get any results from the database as the region cannot be both? If so you'll need to make it so you use "OR" statements for multiple selected regions.
There are numerous ways of completing this. First of all you need a way of dealing with multiple inputs for your region, rather than just defining the one input in your php code. Basically once you have capture your multiple regiones then your query line is the only line that needs to change. This can become:
$sql = mysql_query("SELECT * from gorevler WHERE region IN ('%$bolge%','%$bolge2%','%bolge3%') AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");
Or
$sql = mysql_query("SELECT * from gorevler WHERE (region = '$bolge' OR region = '$bolge2' OR region = '$bolge3) AND country LIKE '%$ulke%' AND sector LIKE '%$sektor%' AND sub_sector LIKE '%$altsektor%' AND product LIKE '%$urun%'");
EDIT with some pointers on your last question:
It just needs to come from your HTML. So if you select more than one region then it will post these to your PHP page. For example:
<option value="" data-filter-type="" selected="selected" id="europe">Europe</option>
<option value="" data-filter-type="" selected="selected" id="usa">USA</option>
then in your PHP
$bolge=$_POST['europe];
$bolge2=$_POST['usa'];
etc.
That's probably the quick and dirtiest method anyway.
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.
I have a form with a unique identifier for each row. I also have a drop down menu with user names.
basically each row has a task and next to each task a drop down menu with name of the task owner. what I need to do is to be able to POST the users name along with their task id so I can update the owner of the task in one submit.
let say the following is what I have in my form
ID Title MENU
1 Task 1 (please select)
2 Task 2 (please select)
3 Task 3 (please select)
make_change
if the user selected "Mike" in row 1 then I want to update the database with Mike's ID which is Mike's option value in the drop down menu.
The thing that I am having a difficulty with in trying to link each menu to the correct row id "task id".
this is how my menu code is displayed
<select name="user_id[]">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
I want to be able to link each menu to the correct task id so when I post the form I can link each menu to the correct row.
How can I include the task id in each menu?
Thanks
So, my understanding is that you need to know which Task ID is related to which select?
Assuming that you create the selects in PHP you could simply change
<select name="user_id[]">
to
<select name="user_id[TASK_ID]">
where TASK_ID is the actual id for the task... (e.g. 1, 2, or 3 etc).
Then when you submit to PHP you could loop through your $_POST[user_id] with for example a foreach loop...
foreach($_POST['user_id'] as $taskid => $userid){
//Do something with the information....
}
UPDATE
If you know the TASK_ID you don't need to iterate through the entire post you can simply access the required field using: $_POST['user_id'][TASK_ID]
For example...
echo $_POST['user_id'][1]; //Echo's the user id for task 1
echo $_POST['user_id'][8]; //Echo's the user id for task 8
Instead of making the name for the select tag an array you can make them unique names. This way each row can be uniquely identified. Example:
<select name="task1">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
<select name="task2">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
This way task1 will map to whatever is selected in the dropdown for task1 and the same for the task2 dropdown.
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.