i'm trying to make a simple webpage for making invoices. to select/load customer info i'm using a select dropdown filled with all the customers in the database.
after selecting a customer i want php to get all the values of that customer from the database and echo somewhere else on the page.
thought it would be something simple but tried everything and cant seem to get it to work.
any help?
after deleting all the code that didn't work anyway this is what i'm left with:
<select name="selectCustomer">
<option selected>Klantnaam</option>
<?
$result = mysql_query('SELECT * FROM '.$c_tbl_name);
while($row = mysql_fetch_array($result)) {
echo '<option value="'.$row['c_id'].'">';
echo $row['c_name'];
echo '</option>';
}
?>
</select>
You need some small AJAX function to do that:
jQuery(document).ready(function($){
$('[name="selectCustomer"]').change(function(){
$('#result').load('load_data_from_db.php', {
customer : $(this).val();
});
});
});
You than need a script load_data_from_db.php which takes the selected customer, generates the content and returns it to the client where it would be placed into an element with the ID result.
Try
$result = mysql_query('SELECT * FROM '.$c_tbl_name) or die(mysql_error());
and this will give info if the query failed.
Furthermore, you should really consider upgrading from mysql() as it is now deprecated. Try google search for 'php PDO'.
Related
I am trying to print the selected dropdwon item. I have already written the code for dropdown to fetch a column from database.
Now i should print the only the id of selcted dropdown item. i don't know how to make it. please help me, this is my code
<?
$query_name="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`name` ASC";
$result = mysql_query ($query_name);
echo "<select name=category value=''></option>";
while($nt=mysql_fetch_array($result))
{
echo "<option value=$nt[name]>$nt[name]</option>";
}
echo "</select>";
$query_id="SELECT id_cat FROM `fs01_metier_cat`";
$result1 = mysql_query ($query_id);
while($row = mysql_fetch_assoc($result1))
{
echo $row['id_cat'];
}
?>
try this:
while($nt=mysql_fetch_array($result)){
echo "<option value='{$nt['id_cat']}'>{$nt['name']}</option>";
}
with {} php can insert also array values into a string and you should set ' ' around the attribute "value"'s value (alot of values here.. ^^), that the html is w3c conform (i dont know if a browser would take it as correct without them..)
without the { } it would look like that:
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['id_cat']."'>".$nt['name']."</option>";
}
depending on your editor code highlighting might work better in the second case ;)
and about the selected item:
i would suggest you to use jQuery to get the content of the selected item
var selectedId = $('#yourselectid option:selected').attr('value');
and then you can e.g. write it to the document or to a div:
document.write(selectedId);
or
$('#yourDivIdWhereYouWantToPutTheSelectedId').html(selectedId);
important: please note that i changed the value's index to id_cat because then you can handle the options with their id
Since the selected option changes everytime you change the dropdown selection, you can not handle this via php. There are ways to do that without a huge library like jQuery (since they are also just simple Javascript) but they simplify such things alot ;)
Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:
<?php
session_start();
?>
<? include ("dbcon.php") ?>
<?php
if(!isset($_GET['coursecode'])){
$Var ='%';
}
else
{
if($_GET['coursecode'] == "ALL"){
$Var = '%';
} else {
$Var = $_GET['coursecode'];
}
}
echo "<form action=\"newq4.php\" method=\"GET\">
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";
$res=mysql_query("SELECT * FROM module GROUP BY mId");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else
{
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>";
for($i=0;$i<mysql_num_rows($res);$i++)
{
$row=mysql_fetch_assoc($res);
echo"<option value=$row[coursecode]";
if($Var==$row[coursecode])
echo " selected";
echo ">$row[coursecode]</option>";
}
echo "</select>";
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" />
</td></tr></table></form><br>";
$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("No modules match your currently selected coursecode. Please try another coursecode!");
} ELSE {
Coursecode: echo $row['coursecode'];
Module: echo $row['mName'];
echo $row['mCredits'];
echo $row['TotalContactHours'];
echo $row['mdescription'];
echo $row['Syllabus'];
}
?>
however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful
Thanks
The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.
Some tips:
1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:
$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
...
}
2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.
For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):
Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:
echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>
Now you have all your drop down options, next is the javascript part
Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:
<div id="course-description"> </div>
<!--style it how you wish -->
With your javascript you could then do this:
$(function(){
$("#id-of-course-drop-down").change(function(){
var desc = $(this).children("option").filter("selected").attr("data-des");
//now you have your description text
$("#course-description").html(desc);
//display the description of the course
}
});
Hope this helps you, even a little
Have fun!
NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)
needing some advice.
I am wanting to include 4 drop down lists on a website, which all contain data from different fields in a mysql table.
I then want to be able to press a submit button and display the required data on a webpage.
I am having trouble with knowing what programming language to use and also finding it difficult to find any tutorials. Any help would be greatly appreciated.
Thanks
You mean a HTML dropdown list or just a select dropdown in a form?
If you mean a select dropdown in a form you could do something like this:
<?PHP
$query = mysql_query("SELECT value FROM table ORDER BY value ASC") or die(mysql_error());
$result = mysql_num_rows($result);
// If no results have been found or when table is empty?
if ($result == 0) {
echo 'No results have been found.';
} else {
// Display form
echo '<form name="form" method="post" action="your_result.php">';
echo '<select name="list" id="lists">';
// Fetch results from database and list in the select box
while ($fetch = mysql_fetch_assoc($query)) {
echo '<option id="'.$fetch['value'].'">'.$fetch['value'].'</option>';
}
echo '</select>';
echo '</form>';
}
?>
And then in your_result.php you should fetch the data from your MySQL database based on value from the (when you fetch use mysql_real_escape_string):
<?PHP $_POST['list']; ?>
You could also do everything in just one file, but thats up to you. Try to use google, there are dozens of tutorials out there.
I'm working on a website and am pulling category names from a db table (category_names). I then display them on the website using php on to an html unordered list. I want to limit the number of category_names and then using jquery(or anything else but I prefer jQuery) retrieve more category_names and add a less button to go back.
I hope I made this question easy to understand, and thank you for any help!
Basically use AJAX to pull in more. Start off by loading in a few by using LIMIT.
<ul id="categories">
<?php
$res = mysql_query('SELECT * FROM `category_names` LIMIT 10'); // change limit to suit
while ($row = mysql_fetch_array($res)) {
echo '<li>'.$row['name'].'</li>'; // or whatever your field is called
}
?>
</ul>
<span id="loadmore" num_loaded="10">Load More</span>
Then use the following jQuery to load more:
$('#loadmore').click(function() {
var loaded = $(this).attr('num_loaded');
$.ajax({
url:'load_categories.php',
type:'get',
data:{'from':loaded,'to':loaded+10},
success: function (res) {
var categories = $.parseJSON(res);
categories.each(function() {
$('#categories').append('<ul>'+this+'</ul>');
});
$('#loadmore').attr('num_loaded',loaded+10);
}
});
});
Finally you'll need to create the PHP page that the AJAX calls - load_categories.php
<?php
if (!isset($_GET['from'])) exit;
if (!isset($_GET['to'])) exit;
$from = $_GET['from'];
$to = $_GET['to'];
$diff = $from-$to;
// connect / select db
$res = mysql_query('SELECT * FROM `category_names` LIMIT '.$from-1.','.$to.';');
$arr = array();
while ($row = mysql_fetch_array($res)) {
array_push($arr,$row['name']);
}
echo json_encode($arr);
?>
There are a number of different approaches that work better or worse depending upon your needs.
Approach 1 (simpler, less efficient, scales poorly): execute the full query, and store all of the results on the DOM, just hiding them using jQuery (jQuery expander is a simple plugin you may want to try out, though I have found it limiting in customization).
Approach 2 (more complicated, but more efficient/scalable, also faster): Use MySQL limit, you can actually send a second mysql request on click, however, you would want to make sure this is asynchronous so as to not delay the user's interactions.
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
This is similar to: PHP/MySQL Show first X results, hide the rest
Or you could do something simpler:
$get = mysqli_queury ($link, "SELECT * FROM db_name WHERE blank='blank' LIMIT 5"
if (isset($_POST['button']; {
$get = mysqli_query ($link, "SELECT * FROM db_name WHERE blank='blank' LIMIT 10"
}
?>
<form action="showmore.php" method="POSt">
<input type="button" id="button" name="button">
</form>
Hope that helps!
Use a datagrid with pagenation. I think datatable JQuery pluggin will work well for you
I'm building a simple web form (or tying to!) which displays a list of football teams. This list of teams is located in a single column mysql table. How can I make it so that if a team has been selected on the web form, that it cannot be selected again on the form? i.e. to make sure they are not accidentally selected to play each other, or play twice at the same time. My code so far is as below, which seems to be working fine. It includes several other iterations of the same code for the other home teams and away teams. Any pointers/help greatly appreciated.
<select name="hometeam">
<?php
$query = "SELECT * FROM teams order by teamname";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n ";
}
?>
</select>
<select name="hometeam2">
<?php
$query = "SELECT * FROM teams order by teamname";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo "<option value=\"".$row['teamname']."\">".$row['teamname']."</option>\n ";
}
?>
</select>
I Method: A possible and simple solution of this can be through javascript. That is, once the selection has been made at both the combo-boxes, then before submitting the form to the server, you can compare the two values and if they are found to be same then you can alert the user about this.
Also, just to add, though you validate using javascript, but its always advisable to re-validate at the server side to avoid any hack. Hope this helps.
II Method: If you don't want to show the team chosen in one drop-down in other drop-down then for this you will have to use Ajax in following manner:
On selection of a value in one drop-down - fire an ajax request - sending the chosen value from the dropdown to the server. Now, modify the query at the server to select all the values excluding this value and then send the values back to the client. This way the user will be able to see the values excluding the chosen one.
Hope this helps.
jQuery would make this quite simple:
<script type="text/javascript">
$(document).ready(function() {
$("select[name=hometeam]").change(function() {
if ( $(this).val() == $("select[name=hometeam2]").val() ) {
alert("This team cannot play itself!");
}
});
$("select[name=hometeam2]").change(function() {
if ( $(this).val() == $("select[name=hometeam]").val() ) {
alert("This team cannot play itself!");
}
});
});
</script>
http://jquery.com/