chained select menu, how to link "submit with a href link? - php

I've been doing a chained select menu by Php, using a database SQL ( as i need later on t develop an update possibility for the client).
However, the chained menu ( at 2 levels) works perfectly,
Problem is I've no idea how to link the button to an other page once submitting . . . and depending of the "items" selected, If anybody can give me some highlite, i'll be really thanksfull.
Here is the Jquery part, which now display the value directly:, instead of this i 'd like to link to an other page.
$("#result").html('Your choice: '+result);
Here is the tutorial I've been following to do it:
http://www.yourinspirationweb.com/en/how-to-create-chained-select-with-php-and-jquery/
THank you so much !!

Firs, if you need to send the user to a different page depending on the selection, you should create a field "destination" where you can save the urls on the database:
CREATE TABLE `type` (
`id_type` int(4) unsigned NOT NULL auto_increment,
`id_cat` int(4) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
`destination` varchar(40) NOT NULL,
PRIMARY KEY (`id_type`)
) ENGINE=MyISAM AUTO_INCREMENT=15 ;
then get the urls from the table and put them on the option value
public function ShowCategory()
{
$sql = "SELECT * FROM category";
$res = mysql_query($sql,$this->conn);
$category = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['destination'] . '">' . $row['name'] . '</option>';
}
return $category;
}
And finaly make a jQuery to redirect to the page on submit
$("#select_form").submit(function( event ) {
var the_url = $("#type").val();
window.location = the_url;
event.preventDefault();
});
Hope that help.

Related

How to show user their submitted form on different page? PHP SQL PDO

I am trying to make an adoption form where the user can see the form submitted.
In the navbar there is a an account page. When you click the account page, I want it listing all the requests submitted by the user.
only want to view the user account information but I get everyone's information in the request table. I think its the line SELECT * FROM request that needs to change but I don't know what too. I don't know what comes after WHERE.
I want the user that has logged in to be able to see the listed requests rather than seeing all requests made by every user which is what i get currently.
SQL Database
---login details---
CREATE TABLE `account` (
`id` int(11) unsigned NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(250) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
---adoption Request---
CREATE TABLE `request` (
`id` int(11) unsigned NOT NULL,
`fullname` varchar(30) DEFAULT NULL,
`email` varchar(120) DEFAULT NULL,
`address` varchar(120) DEFAULT NULL,
`postcode` varchar(30) DEFAULT NULL,
`present` varchar(120) DEFAULT NULL,
`category` enum('Snake','Rabbit', 'Hamster', 'Dog', 'Kitten') NOT NULL DEFAULT 'Snake',
PRIMARY KEY (`id`),
userId INT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Account Listing Code
<div class="wrapper row3">
<main class="hoc container clear">
<!-- main body -->
<div class="sectiontitle">
<h6 class="heading">Account Information</h6>
<p>Adoption Request Form</p>
</div>
<?php
include("connectdb.php");
?>
<table cellspacing="0" cellpadding="10">
<tr><th>fullname</th> <th >email</th><th >address</th> <th >postcode</th><th >present</th><th >category</tr></tr>
<?php
try{
// Run a SQL query
$sqlstr = "SELECT * FROM request";
$rows=$db->query($sqlstr);
//loop through all the returned records and display them in a table
foreach ($rows as $row) {
echo "<tr><td >" . $row['fullname'] . "</td><td >" . $row['email'] . "</td><td >" . $row['address'];
echo "</td><td >" . $row['postcode'] . "</td><td >" . $row['present'] . "<tr><td >" . $row['category']."</td></tr>\n";
}
echo "</table> <br>";
} catch (PDOException $ex){
//this catches the exception when the query is thrown
echo "Sorry, a database error occurred when querying the vehicle records. Please try again.<br> ";
echo "Error details:". $ex->getMessage();
}
?>
</div>
Read up on entity-relationship data modeling. Seriously. Go and do it.
Your entities (tables) are, I believe,
account with one row per human user
critter with one row per creature to be adopted
request with one row per adoption request
Each of these tables contains attributes of the entity. account.email and account.firstname for example. critter.species and critter.is_adopted_yet for example.
Each table will have an autoincrementing primary key account_id, critter_id, request_id.
request will contain a critter_id and an account_id as well as the attributes describing the request. That is, each row of request represents a relationship between an account and a critter.
Then, when looking for information about a single account's requests, you'll do something like this.
SELECT request.account_id, critter.*
FROM request
JOIN critter ON request.critter_id = critter.critter_id
WHERE request.account_id = ?
Using a WHERE filter to pull out just the account you want.

Delete from database doesn't work correctly

So i have Many-to-many extra table and i want to delete a row from the extra table .
CREATE TABLE `person_cars` (
`person_cars_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`person_id` int(10) NOT NULL,
`car_id` int(10) NOT NULL,
PRIMARY KEY (`person_cars_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
This PHP doesn't work
if(isset($_GET['id']) && isset($_GET['person_cars_id'])) {
$person_id = $_GET['id'];
$person_cars_id = $_GET['person_cars_id'];
$person->deleteCarOfPerson($person_cars_id);
header('location:join.php?id=' + $person_id);
}
SQL
public function deleteCarOfPerson($person_cars_id) {
$sql = ("DELETE FROM person_cars
WHERE person_cars_id = '{$person_cars_id}'");
$result = mysql_query($sql, $this->mysql_database->getConnection());
return $result;
}
Now delete is working but not header('location:join.php?id=' + $person_id);
if(isset($_GET['person_cars_id'])) {
$person_id = $_GET['id'];
$person_cars_id = $_GET['person_cars_id'];
$person->deleteCarOfPerson($person_cars_id);
header('location:join.php?id=' + $person_id);
}
Your re-direct isn't working as you're mixing PHP with Javascript.
Change
header('location:join.php?id=' + $person_id);
to
header('Location: join.php?id='.$person_id);
+ is used in Javascript to add something.
Whenever use header function you should use exit also after header redirection.
header("lOCATION:JOIN.PHP");
EXIT;

Create a new page on submit from database

I've created a chained menu using php and a database, following this tutorial.
The first table content a list of categories like :
CREATE TABLE IF NOT EXISTS `chainmenu_categories` (
`id_cat` int(4) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(40) NOT NULL,
PRIMARY KEY (`id_cat`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=12 ;
My second table, the type, like :
CREATE TABLE IF NOT EXISTS `type` (
`id_type` int(4) unsigned NOT NULL AUTO_INCREMENT,
`id_cat` int(4) unsigned NOT NULL,
`name` varchar(40) NOT NULL,
`destination` varchar(40) NOT NULL,
PRIMARY KEY (`id_type`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
I managed, once clicking on submit, to redirect to another page by that in my select.php:
var result = $("select#type option:selected").html();
$("#select_form").submit(function( event ) {
var the_url = $("#type").val();
window.location = the_url;
event.preventDefault();
});
and adding this on my select.class.php
public function ShowCategory()
{
$sql = "SELECT * FROM chainmenu_categories";
$res = mysql_query($sql,$this->conn);
$category = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['id_cat'] . $row['destination']. '">' . $row['name'] . '</option>';
}
return $category;
}
So now it redirects each time to a different page depending of the option choose from the menu, like http://mydomain.com//3 then 4, 5, 6, etc.
But as the page doesn't exist, it redirects to a dead link.
Can someone give me help to create these pages from the chained menu (or have some highlight)? and if possible, some pointer to create the admin interface to allow an admin to add the pages and categories to the chained menu?
I've been trying to start with something which look like:
PHP Code:
<?php require('db_config.php');
$stmt = $db->prepare('SELECT id_type, name FROM type WHERE id_cat=$_POST[id]');
$stmt->execute(array(':id_cat' => $_GET['name']));
$row = $stmt->fetch();
However, I don't know if this is good at all.
if(file_exists($filename.'.php'))
echo "file : " . $filename.'.php' . " is exist";
else
{
$file = fopen($filename.'.php',"w");
$code="here what U want to write inside the new page.php";
fwrite($file,$code);
fclose($file);
}

Populating Multiple Combo Boxes with PHP from MySQL database

I have a maximum of 100 selections (now 79) for the combo box, and have 79 of them. Each one stands for a question and is recorded to "questions" table in MySQL. The options are recorded in a table called "question_codes" and they stand for the field names associated with some questions. I tried 2 for loops within each other but I think that will overload the server because of 79*79 iterations. I also have a text field depending upon the selection with ajax, working properly. Number 0 field in the table "question_codes" is id and I don't use that. Here is my code.
$code_query="SELECT * FROM questions WHERE id='1'";
$question_query="SELECT * FROM questions WHERE id='2'";
$result_question_query=mysql_query($question_query,$conn);
$result_code_query=mysql_query($code_query,$conn);
$selected_query=mysql_query("SELECT * FROM question_codes");
while($row_selected=mysql_fetch_row($selected_query))
{
$column_count=count($row_selected);
}
while($row=mysql_fetch_array($result_question_query) && $row2=mysql_fetch_array($result_code_query))
for($i=1;$i<$column_count;$i++)
{
$db_q="q$i";
$fill=$row[$db_q];
$fill_code=$row2[$db_q];
print('<tr style="background:navy;color:white"><td width="60px">Question '.$i.'</td>
<td>
<select name="'.$db_q.'_code" id="'.$db_q.'_code" onchange="showQuestion(this)">
<option value="">Select a question</option>
for ($j=1;$j<$column_count;$j++)
{
$name=mysql_field_name($selected_query,$j);
if ($fill_code==$name)
{
$selected="selected";
}
else
{
$selected="";
}
print('<option value="'.$name.'" '.$selected.'>'.$name.'</option>');
}
print('</select>
</td>
<td><input type="text" value="'.$fill.'" name="'.$db_q.'" id="'.$db_q.'"></td></tr>');
There's probably a more elegant way to structure the database and PHP.
Perhaps something like this for SQL:
CREATE TABLE `questions` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question` varchar(255) NOT NULL,
`answer` int(11) unsigned NOT NULL DEFAULT '0'
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `question_options` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`question_id` int(11) unsigned NOT NULL DEFAULT '0',
`option` varchar(64) NOT NULL
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
... and this for the PHP ...
$q_sql = mysql_query("SELECT * FROM `questions`");
while ($q = mysql_fetch_row($q_sql)) {
echo '
<tr><td>Question '. $q['id'] .'</td>
<td><select name="q'. $q['id'] .'">';
$qo_sql = mysql_query("SELECT * FROM `question_options` WHERE `question_id` = '". $q['id'] ."'");
while ($qo = mysql_fetch_row($qo_sql)) {
$selected = ($q['answer'] == $qo['id']) ? ' selected' : '';
echo '
<option value="'. $qo['id'] .'"'. $selected .'>'. $qo['option'] .'</option>';
}
echo '
</select>
</td></tr>';
}
To tighten this further (this method can generate too many queries) ... make a single query instead, something like:
SELECT q.`question`, qo.*
FROM `question_options` as `qo`
LEFT JOIN `questions` as q
ON (q.`id` = qo.`question_id`)
ORDER BY qo.`question_id`
... then with a single while loop, check for a change in the question.

How can I speed up a MySQL product search result?

I am doing a query on an mysql database based on typed data sent from a jquery autocomplete function. It works fine but take several seconds (5-6+) to get the returned suggestions. There are around 200,000 product entries in the database currently which is expected to go to well over a million entries. How can I speed this thing up, as once more entries are added, it stands to reason that the results would likely never show before someone has already typed in the full search query, which does not benefit them at all.
The MySQL tables are:
CREATE TABLE `products` (
`id` int(12) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`description` text NOT NULL,
`category` int(12) NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `name` (`name`),
KEY `category` (`category`)
) ENGINE=MyISAM;
CREATE TABLE `products_pics` (
`id` int(12) NOT NULL auto_increment,
`product_id` int(12) NOT NULL default '0',
`thumb` blob NOT NULL,
`image` longblob NOT NULL,
`type` varchar(6) NOT NULL default '',
PRIMARY KEY (`id`),
KEY `product_id` (`product_id`),
) ENGINE=MyISAM;
The PHP code for the search and result return is:
if(isset($_REQUEST['queryString'])) {
$queryString = addslashes($_REQUEST['queryString']);
if(strlen($queryString) > 0) {
$result = mysql_query("SELECT * FROM products WHERE name LIKE '%" . $queryString . "%' ORDER BY name LIMIT 8", $db);
if(mysql_num_rows($result) > 0){
while($myrow = mysql_fetch_array($result)){
$query = mysql_query("SELECT id FROM products_pics WHERE product_id='$myrow[id]' LIMIT 1", $db);
if(mysql_num_rows($query) > 0){
echo '<a href=""><img src="/ajax/search_images.php?id=' . $myrow[id] . '" width="55" border="0" alt="" />';
}
else {
echo '<a href=""><img src="/images/items/no-img.jpg" width="55" border="0" alt="" />';
}
$name = stripslashes($myrow[name]);
if(strlen($name) > 35) {
$name = substr($name, 0, 35) . "...";
}
echo '<span class="searchheading">' . $name . '</span>';
$description = stripslashes($myrow[description]);
if(strlen($description) > 80) {
$description = substr($description, 0, 80) . "...";
}
echo '<span>' . $description . '</span></a>';
}
echo '<span class="seperator"></span>';
}
}
}
Thanks again guys, any help is appreciated.
Doing LIKE %keyword% is very slow. Because you are doing autocomplete you could discard the first %, doing: LIKE keyword%.
That will speed up your query dramatically.
Also, try to execute your query in MYSQL like 'EXPLAIN <>'. This will provide you with info on how MySQL executes your query and where you might optimize.
Another option might be to use MyISAM's FULL TEXT SEARCH capability: http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html

Categories