Posting info on a page depending on value chosen in selectbox - php

I am working with creating an ad posting page. I have included dependable select boxes as the category selection system. All the values are being pulled from MySQL database. The table is composed of an id (auto increment) bridged by master_id and identified by category name. Every category has a subcategory. So the user is forced to pick a subcategory before being able to submit the info. I am currently looking for ideas of how to post info that has been input by user depending on the category chosen. I have created an individual page for each subcategory. So every time an input is submitted, I want it to display in the page accordingly. For example, if I pick category: Books and subcategory:Textbooks then post it in textbooks.php. Live EXAMPLE
Table Structure
Drop down menus
<form action="" method="post">
<select name="category" id="category" size="7" class="updateCategory" >
<option selected="selected" value="">Select one:</option>
<?php if (!empty($list)) { ?>
<?php foreach($list as $row) { ?>
<option value="<?php echo $row['id']; ?>">
<?php echo $row['name']; ?>
</option>
<?php } ?>
<?php } ?>
</select>
<select name="subc1" id="subc1" size="7" class="updateCategory"
disabled="disabled" hidden="hidden" >
<option value="">----</option>
</select>
<select name="subc2" id="subc2" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
<select name="subc3" id="subc3" size="7" class="updateCategory"
disabled="disabled" hidden="hidden">
<option value="">----</option>
</select>
</br>
Insert your name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>

This is a little bit of a shot in the dark, as the question was vaguely worded.
I think that, first, you will need to make an update to the database. You will need to create either a new table or a new column. I would personally choose the table as that provides more flexibility, like so: id, category_id, category_page (and you could put other columns in too). Obviously, category_id references back to the main table category.
Then, do a select like this:
SELECT [COLUMNS HERE] FROM category RIGHT JOIN category_page ON category_page.category_id = category.id
You now have the values you need.
Then, in your PHP, return the url to the page (if you are using JS to dynamically show the new page):
<option value="xyz" data-url="url-for-page.php">Category Name</option>

Related

Whats the possibility of one updating a database field with a form having a drop down without changing the value of the drop down field

Lets say i input data into a mysql database using a drop down like:
<div>
<input name="name" type="text" id="name" placeholder="Enter Name"></div>
<div>
<select name="position" id="position" type="text">
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
With a normal text field, i can simply include the value field to show on the form the initially entered data in the table. And change that so when i submit form, it updates the related field.
<div>
<select name="position" id="position" type="text" value="<?php echo $row_position['Position']; ?>>
<option selected="selected">Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
So assuming i do not want to update the field relating to the drop down, how do i it? Because what happens is, once i submit the form without selecting any option from the drop down, it picks the first option from the list which is the "Select" option and uses that to update the related field in the database.
I'd suggest at least two steps to make it user-friendly and achieve your goals:
First, make the initially entered selection selected if available:
<?php
$positions = Array("A", "B", "C");
?>
<div>
<select name="position" id="position">
<option value="-1">Select</option>
<?php
foreach($positions as $v) {
$selected = ($row_position['Position']===$v) ? "selected" : "";
echo "<option value='$v' $selected>$v</option>";
}
?>
</select>
</div>
And on the receiving php-script check if you've received a valid option, otherwise send error-message:
<?php
//other stuff...
if($_POST['position']>0) {
// save that value
} else {
// send error to user
}
// even more stuff..
?>
Notes: in select tag there is no type=text nor a value=anything available.
Assuming you are fetching dropdown options from database.
Note: Array keys, variables are only for demonstration purposes. This may differ from your actual records.
<div>
<select name="position" id="position">
<?php foreach($options as $option): ?>
<option value="<?= $option['position'] ?>" <?php if($option['position'] == $current_record['position']) { echo "selected"; } ?>> <!-- e.g. $option['id'] -->
<?= $option['name'] ?>
</option>
<? endforeach; ?>
</select>
</div>
What i have done that has solved the issue was to simply add the value from the database to the selectedoption or the first option field. So by default without changing the drop down, the first option with the value from the database is selected. And so there would be no change.
<div>
<div><?php echo $row_position['Position']; ?></div>
<select name="position" id="position">
<option value="<?php echo $row_position['Position']; ?>Select to Change</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
</div>
This gives a simple or straight forward solution to the problem.
Will still try the other suggestions to see how it all plays out. Thanks guys.

Show All Categories in "Form" Select Option Dropdown

I have about 10 categories and building a search form, But i want to show all categories & even a single category in option. How i can do that? So user can search in all categories with a single phrase.
<input type="text" class="form-control" value="" name="searchterm" id="searchterm" placeholder="Start Searching..." style="width: 60% "/>
<select id="fcat" class="form-control" name="catname" style="width:40%;" required />
<option value="">Select Category</option>
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo '<option value="">All Categories</option> <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>'; } ?>
</select>
That would be more helpful if you guys could tell me a mysql query to search with a phrase in all categories. I have another table called companies and categories so if someone is search a phrase like "some business name" and select all categories then should show all companies within all categories.
Thanks in advance...
Remove 'All Categories' option from while loop.
Add some value for 'All Categories' option.
In the form action page based on the select box option write/generate a query.
Form code
<?php $fetch_category = mysql_query("SELECT * FROM category ORDER BY categoryname ASC");
while($throw_category = mysql_fetch_array($fetch_category)) {
echo ' <option value="'.$throw_category['categoryname'].'">'.$throw_category['categoryname'].'</option>';
} ?>
<option value="all">All Categories</option>
</select>

UPDATE using html form (edit button) php/mysql

I'm trying to figure out how to UPDATE a post in my db using html form. I have a page with a form using INSERT that successfully uploads data to my db. I also have another page presenting that data using SELECT. On this page I would like to have an Edit button, sending me to a similar page with the original form but this with the data from the db already inserted into the form, for easy editing and updating. Basically I don't know where to start and I need your help! It's an album database btw.
form page
<form method="post" action="spara2.php" name="std">
<label>Cover:</label><br>
<input id="file" type="file" name="file"><br>
<label>Title:</label><br>
<input type="text" name="std_title" size="58"/><br>
<label>Artist:</label><br>
<input type="text" name="std_artist" size="58"/><br>
<label for="select"><br>
Year:</label><br>
<select name="std_year">
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
</select><br>
<label>Description:</label><br>
<textarea name="std_beskriv" cols="45" rows="7">
</textarea><br>
<label>Tracks:</label><br>
<textarea name="std_lista" cols="45" rows="7">
</textarea><br>
<label>Spotify HTTP Link:</label><br>
<input type="text" name="std_spotify" size="58"/><br>
<br>
<label for="reset"></label><input type="reset" value="Reset"/>
<label for="save"></label><input type="submit" name="std_save" value="Save"/>
</form>
My db is called album and the table musik. I want a similar page but with the data from the db inserted into it. My table has:
id
title
artist
year
beskriv
tracks
spotify
date
Since there's a lot of posts in the db how can I select specific entry's so that I can edit one album at a time?
Hope you can understand what i'm after.
Thanks!
You should use $_GET to get a specific album from the DB, and use the attributes as default values in the form. Here's an example.
Album1 Edit
Album2 Edit
Album3 Edit
Album4 Edit
Album5 Edit
Album6 Edit
When you retrieve all the albums from db, you should use the unique id of each album to create a unique path to update a specific album. So the Edit button should have a link of this form:
<a href="edit.php?albumID=<?php echo $album[id];?>">
So after writing this, you will have for the first album edit link: edit.php?albumID=1
To create the edit.php page, first you should fetch from the DB the album by specifiying the ID passed by the $_GET['edit'] parameter in the url. So this will look like:
$albumid = mysql_real_escape_string($_GET['edit']);
$sql = "SELECT * FROM musik where albumid = $albumid";
If the record was not found, redirect to a 404 page. Otherwise, echo the attributes in the corresponding inputs.
Ex:
<input type="text" name="std_artist" value="<?php echo $album['std_artist'];?>" size="58"/>

Change the list of dropdown according to the selection of other dropdown

I have two dropdowns .i want second dropdown list shoul changed according to the value selected in first dropdown.
this is my first dropdown
Category :<select name="Category" id="a1_txtBox5" required="required">
<option value="select">select..</option>
<?php while($selectcategoryarray=mysql_fetch_array($selectcategory)) {
?>
<option value="<?php echo $selectcategoryarray[1];?>"><?php echo $selectcategoryarray[1];?></option>
<?php
}
?>
</select>
And here is my second dropdown:
<label style="margin-left:24px;">Subcategory :</label><select style="margin-right:35px;" name="subcategory" id="a1_txtBox3" required="required">
<option value="select"> select..</option>
<?php while($selectsubcategoryarray=mysql_fetch_array($selectsubcategory)) {
?>
<option value="<?php echo $selectsubcategoryarray[2];?>"><?php echo $selectsubcategoryarray[2];?></option>
<?php
}
?>
</select>
Please help.
Exactly you need to handle the Change Event for your first Select element and in the body of the event you need to send request to server for getting data of second Select element.
I recommend to use an ajax process to doing this.
And o this you should use jQuery for handling events and have ajax.

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