I am creating a php search. I select the 2 menus result from the database. The first menu works fine, but the second one no. Can you please help me?
This is my home.php
<?php
require("ConfigPage.php");
$sql="SELECT LocationName
FROM tblplacelocation
Join tblplace ON tblplace.PlaceID=tblplacelocation.PlaceID
Join tbllocation ON tbllocation.LocationID=tblplacelocation.LocationID
where PlaceType='Hotels'" ;
$result = mysql_query($sql,$con);
echo '<form><P style="color:white;">Region</P>
<select name="PlaceID" onchange="showUser(this.value)">
<option value="">Region:</option>';
while($data = mysql_fetch_array($result))
{
$id= $data['PlaceID'];
$lname = $data['LocationName'];
echo "<option value='". $lname. "'>".$lname."</option>";
}
echo "</select></form>";
echo "<div id='txtHint'></div>";
echo "<div id='divImage'></div>";?>
this is the getuser.php
<?php
$q = intval($_GET['q']);
include("ConfigPage.php");
$sql="SELECT PlaceName
FROM tblplacelocation
Join tblplace ON tblplace.PlaceID=tblplacelocation.PlaceID
Join tbllocation ON tbllocation.LocationID=tblplacelocation.LocationID
WHERE LocationName = '".$q."' AND PlaceType='Hotels'";
$result = mysql_query($sql,$con);
echo '<form><P style="color:white;">Place Name</P>
<select name="adminID" onchange="showImages(this.value)">
<option value="">Select a place:</option>';
while($data = mysql_fetch_array($result))
{
$pid= $data['LocationName'];
$pname = $data['PlaceName'];
echo "<option value='". $pid. "'>".$pname."</option>";
}
echo "</select></form>";
mysql_close($con);
?>
and this is the java script:
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
I don't know what's wrong. The first menu displays the result but the second menu contains nothing.
I would suggest you to use Firebug for debugging.
check if you have any javascript errors (maybe syntax error)
check if your ajax request is being send, if yes, check what's inside the response
check if your dom element is being selected correctly ... in firebug you can execute javascript in the console area. check if your document.getElementById selects the correct element by inserting something yourself
if all of this works, check if your select element has empty options. if this is the case, your variables should be empty
if your select element has no options, something with your sql select may be wrong
If you're just experimenting, it's ok, but otherwise let me say that your code looks pretty ugly. This is not the way you should programm things.
You should think about dividing your code by concerns. Check out the Model-View-Controller Pattern and beginner tutorials for any PHP-Framework.
MVC:
http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
A Framework which is easy to learn:
http://www.yiiframework.com/
Related
I'll get straight to the point.
I have a a list which shows everyone in the db who has confirmed that they'll be attending a match. I want the list to change depending on what group is chosen from a select box.
Here's what I have so far: html & AJAX
<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("attendYes").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("attendingYes").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","attending.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a group:</option>
<option value="CMA">CMA</option>
<option value="CM1">CM1</option>
<option value="CP2">CP2</option>
<option value="CBBC">CBBC</option>
</select>
</form>
<br>
<div id="attendYes"><b>Person info will be listed here...</b></div>
</body>
</html>
And here is the 'attending.php' page that the choice is meant to be sent to:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('-----','-----','-----','-----');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"handsomejack_co_forms");
$sql="SELECT * FROM stats WHERE activity='".$q."' AND attend='0' ORDER BY username ASC";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>User</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I have changed the sql from ".$q." to a defined choice - like CMA - and it worked, so I know the issue comes from sending q over.
Any ideas as to what could be the problem?
Cheers.
There are two three things i would suggest:
A.)
Change the id of the target div. as you have attendYes and you are using txtHint.
B.) # js
You can try sending the query in the .send() like below.
xmlhttp.open("GET","attending.php",true);
xmlhttp.send("q=" + encodeURIComponent(str));
C.) and # php:
You should not use intval() although i am not very good at php but as per documentation it is something else. so i suggest you to change this:
$q = intval($_GET['q']);
to this:
$q = $_GET['q'];
There's no element like document.getElementById("txtHint"). Update it with correct one:
document.getElementById("attendYes").innerHTML = xmlhttp.responseText;
Why don´t you use Jquerys $.ajax Method instead.
It´s easier to implement and it´s runing in all Major Browsers.
i have this code below of javascript:
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","includes/get_user.php?q="+str,true);
xmlhttp.send();
}
</script>
and the php sql script is given below:
<?php
$testQrys = "SELECT * FROM test where status = 1";
$testdbResults = mysql_query($testQrys);
?>
<select size='5' width='925' style='width: 925px' name='users' onchange='showUser(this.value)' multiple>
<?php while($test = mysql_fetch_array($testdbResults )) { ?>
<option class='h4' value='<?php print($test[9]); ?>'>
<?php print($test[5]);echo" ( ";print($test[9]);echo" )"; ?>
</option>
<?php } ?>
</select>
<div id="txtHint"></div>
and the get_user.php code is:
<?php
$q=$_GET["q"];
$con = mysqli_connect('localhost','root','','airways');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM test WHERE m_email = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<input type='text' name='staff_no[]' value='".$row['m_staff_no']."'>";
}
mysqli_close($con);
?>
now what i want is when i select one user in the select option it shows the staff no of that user but when i select multiple users it does not show the staff no of other users i select.
please help me with the change in code so i can get the staff no of users like (22344, 44333, 33344, 55443, 11125, 25263) in the text box
waiting for the kind and prompt responses.
Thanks in advance
The error is coming from showUser(this.value). This returns only the first selected element's value. You need to cycle through all options and concatenate them together to make a string that you will be able to send as a parameter.
Change your javascript code to something along those lines. Note that you will need to change your PHP code in order to separate the emails, sanitize them and create a working WHERE m_email IN () query.
You will need to add id="users" to your HTML code first.
var usersList = document.getElementById('users');
var emailString = '';
for (user_counter = 0; user_counter < usersList.options.length; user_counter++) {
if (usersList.options[user_counter].selected) {
emailString += usersList.options[user_counter].value;
}
}
Then send emailString as the parameter to the PHP script. Test it again with log.txt in case you get an error.
I have a simple (or so I thought) PHP, MYSQL, AXAX system set up to register votes for items on my website. I'm only doing upVotes so there's only one button.
My issue is that when the page loads, the correct number of votes for the item is displayed, but upon the first click of the upVote button, the value does not change. I know I must be missing something, but I have no idea what. It's probably simple... but I just can't figure it out.
Everything connects to the DB just fine. That's why I left all that code out.
Any help is greatly appreciated.
This is the javascript I have:
<script type="text/javascript">
function voteButton(str)
{
if (str=="")
{
document.getElementById("voteUp").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("voteUp").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","vote?id="+str,true);
xmlhttp.send();
}
</script>
This is the php file used to query the database:
<?php
$id=$_GET["id"];
include("myDatabaseStuff.php");
include("global.php");
$result = mysql_query("UPDATE TABLE SET up_mod = up_mod + 1 WHERE img_id = $id");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($getUpVoteResult))
{
echo $row['up_mod'];
}
?>
This is the HTML I use on the page to display the values/button:
<div class="span1" style="margin:30px 0 0 10px;">
<button onclick="voteButton('<?php echo $id;?>')" class="btn btn-large" type="button" style="margin-top:5px"><div id="voteUp"><?php echo $votes[0] ?></div><i class="icon-thumbs-up"></i></button>
</div>
Add:
$getUpVoteResult = mysql_query("SELECT up_mod FROM table WHERE img_id = $id") or die ("Invalid query: " . mysql_error());
before the while loop.
You also don't need a while loop, since there's just one row.
After your update, please do the separate SELECT.
I have a form where radio types are generated inside a PHP while loop. Depending on which radio is selected, java script queries MySQL to get the correct value and return it to a text box. It works except for onload. OnLoad returns the incorrect value, whatever was first in the while loop iteration. Onchange will return the right value.
How can I get onload to return the right value? I am using "checked" in my radio throughout the while iteration so that one circle is always checked. It doesn't matter to me which radio is checked by default. Using checked in the while iteration always makes the last iteration checked which is OK, but it is displaying the value for the first.
here is my code.
<script type="text/javascript">
function getNextcheck(str)
{
if (str=="")
{
document.getElementById("nextcheck").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("nextcheck").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getnextcheck.php?id="+str,true);
xmlhttp.send();
}
window.onload = function() {
document.getElementsByName('ID')[0].onchange();
}
</script>
And the file getnextcheck.php it pulls
<?php
include $_SERVER['DOCUMENT_ROOT']."/connect.php";
$result = mysql_query("SELECT * FROM bankaccount WHERE ID = '$_GET[id]'");
$row = mysql_fetch_array($result);
$nextcheck = $row['next_check'];
if (empty($nextcheck)) { $nextcheck = 0; }
echo "<tr><td>Check Number <input type=\"text\" name=\"checkno\" value=\"".$nextcheck."\" /></td></tr>";
And the radio inside the form.
while($row2 = mysql_fetch_array($result2)) {
$id = $row2['ID'];
echo "<tr><td><input type=\"radio\" name=\"ID\" value=\"".$row2['ID']."\" onchange=\"getNextcheck(this.value)\" checked></td><td>".$row2['name']."</td><td>".$row2['acctnum']."</td>";
}
Thank you for your help.
make only first radio check
<?
$i=0;
while($row2 = mysql_fetch_array($result2)) {
$id = $row2['ID'];
$checked = $i?'':'checked';
echo "<tr><td><input type=\"radio\" name=\"ID\" value=\"".$row2['ID']."\" onchange=\"getNextcheck(this.value)\" {$checked}></td><td>".$row2['name']."</td><td>".$row2['acctnum']."</td>";
$i=1;
}
?>
I have a php script which has series of calculations and FORM RELOADS based on user Selection/Inputs, I need to run these only IF a record ADDITION is done to the table and not on FORM RELOADS, how to run a loop with this condition.
my PARTIAL php script bill-detail.php:
HOPE THIS WOULD SUFFICE:
<html>
<head>
<title>Cash Bill Detail</title>
<script language="javascript">
function reload(form)
{
document.location.href = 'bill-detail.php'
}
function showecr(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","billecr.php?q1=" + str,false);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
$con = mysql_connect("localhost","svga","a!##");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("svga", $con);
$user = $_SESSION['user'];
//<!--------------VALIDATIONS ------------------->
echo "<form name='form1'>";
echo "ECR: <input type='text' size='10' maxlength='10' name='ecrn' onchange=\"showecr(this.value);\"><br>";
echo "Product Type";
echo "<select name='product' onchange=\"reload(this.form);\">>";
echo "<option value='Gas'>Gas</option>";
echo "<option value='Others'>Others</option>";
echo "</select>";
echo "</form>";
//---As per user inputs copying value to variables---------------------
$hdr=mysql_query("SELECT * FROM BILLHDR_draft WHERE usr='$user'");
$hdr2=mysql_fetch_assoc($hdr);
$numrows=mysql_num_rows($hdr);
if ($numrows>0)
{
$cust_code=$hdr2['Ac_code'];
$product=$hdr2['prod_desc'];
//--Searching BILL DETAILS TABLE as per the user inputs from BILL HEADER Table------
$recs=mysql_query("SELECT * FROM BILLDTLS WHERE Ac_code='$cust_code' AND Prod_desc='$product'");
while ($rec2=mysql_fetch_assoc($recs))
{
mysql_query("INSERT INTO DEMURAGE (DC_No, DC_date, Ac_code, Product_Desc) VALUES ('$rec2[dc]', '$rec2[dcdate]', '$rec2[accode]', '$rec2[product]'");
}
}
mysql_close($con);
?>
You can add a boolean variable before the loop that you can use to track on whether a record has been added already. The following code is largely concept being that you didn't provide much of an example of code yourself, but hopefully you get the idea.
// Set to false initially
$recordAdded = FALSE;
if( $num_rows > 0 && $recordAdded == FALSE )
{
// Code to execute
// Set value to to TRUE after initial successful execution
$recordAdded = TRUE;
}