AMENDMENT:
Hi all, thanks for all your replies. I've been wrestling trying to shoe horn the code into my page but I've still had no luck. While fiddling around I can't see why something like the below code wouldn't work - I can't get this to work either mind you(!). If it's not possible I'm hoping someone can shoot the idea down to save me wasting time trying to do it:
<form name='Names' role='form' action='#' method='get' onchange='this.form.submit()'>
<select name="FirstName">
<option value="1">FirstName#1</option>
</select>
<select name="LastName">
<option value="1">LastName#1</option>
</select>
</form>
...could I then pick up these selections in $_GET['FirstName'] and $_GET['LastName'] and pass them into my mysqli_query() to generate the table?
To prevent any chance of my confusion spreading I first want to describe what I'm trying to do... I want a webpage that has a table with a group of dropdowns that I can filter on - similar to Excel filters. If you know of a better way of doing it than how I'm trying below - please do enlighten me!!
Now, to describe the best progress I've made so far:
I have the below script (copied straight over from W3schools:
<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","entry_table.php?q="+str,true);
xmlhttp.send();
}
</script>
The above script is called from the below dropdown which populated using a PHP while loop from an SQL query:
<form>
<select name="Name" onchange="showUser(this.value)">
<option value="1">User#1</option>
</select>
</form>
The $q value is then passed to a PHP page to display the table after generating the SQL query with a:
WHERE UserID="$q"
This works but I'm concerned it seems to be a long-winded way of doing it - especially if I'm try to and expand the queries to include 6-7 other dropdown filters.
Thanks in advance,
Ben
You could go for a callback function instead.
function getData(url, callback)
{
if (window.XMLHttpRequest)
{
//IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{ //IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
And then make an ajax call like this:
getData("entry_table.php?q="+str, function(data)
{
document.getElementById("txtHint").innerHTML=data;
});
Outer function
function doStuff(value) {
getData("entry_table.php?q="+value, function(data)
{
document.getElementById("txtHint").innerHTML=data;
});
}
and then select would be
<select onchange="doStuff(this.value)">
Related
(Re Post) i had going through this tutarial : PHP ajax database : how to pass two variables and get data of them in different div even one? ,but it seems not working,and i need to have 3 select works together.i choose first select option then using ajax to pass data for getting second and using the
function showUser(strOther);
to get third data which is related to the first and second answer.all is running well but the third select which is:
<select id="txtHint1">
doesnt show me any answer.here is my scripts:
<script>
function showForum(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();
}
</script>
<script>
function showUser(strOther) {
if(strOther==""){
document.getElementById("txtHint1").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("txtHint1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q1="+strOther,true);
xmlhttp.send();
}
</script>
this is the select options which i get the ajax data:
<!-- this forum will be
choosed to pass data to get second
select to filled up-->
<form>
<!-- first select-->
<select name="users" onchange="showForum(this.value)">
<option value="">All Orgs</option>
<option value="1">WebStatsProject</option>
<option value="2">mmu</option>
</select>
</form>
this select option will be filled by the first ajax request:
All Forums
the third not show any data,the other two working properly:
<!-- third select-->
<select id="txtHint1">
<option value="All Users">All Users</option>
</select>
Because you not define strOther
try this
function showUser(str,strOther){
and pass strOther to this function
So I am trying to put the GET of the ajax request in the <select> using an id but it doesn't work. However when I use for example <p> with an id or <option> or anything else it does work and output what I want.
This is my Ajax script
<script>
function showUser(str)
{
if (str=="")
{
document.getElementById("brand").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("brand").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","inc/parts_form2.php?q="+str,true);
xmlhttp.send();
}
This is a part of the form
Year:
<select onchange="showUser(this.value)">
%year%
Brand:
<select id="brand">
</select>
And this is my php
<?php
$q = $_GET['q'];
echo $q;
?>
I am trying to get the output in <select id="brand"></select>. Which I check in the source code if it worked.
You need to put the content in option tags inside the select tag.
"<option>"+xmlhttp.responseText+"</option">;
or in php :
$d = "<option>".$_GET['q']."</option">;
Isnt the response text passed into the onreadystatechange callback?
xmlhttp.onreadystatechange = function (response){
// insert your method body here
}
I have a page that lists customers from a SQL database. It lists the credits they have left and I have a button that I can click to remove one credit. The way it is done is when you click on the button it calls a Ajax function that runs a php page that remopves one credit and echoes the credit after that.
The php page works fine when I input the string in the URL manually but smy Ajax function must be wrong.
here is the listing with the form:
while ($data = mysql_fetch_object($result)) {
print "<TR><TD>".$data->pass_name."</TD><TD><span id='credit'>".$data->credit_left."</span></TD><TD><form><input type='submit' value='- 1' onsubmit='removeOneCredit(pass_id=".$data->pass_id."&credit_left=".$data->credit_left.")'></form></TD></TR>\n";
}
and here is my function:
<script>
function removeOneCredit(str)
{
if (str=="")
{
document.getElementById("credit").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("credit").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","removeonecredit.php?"+str,true);
xmlhttp.send();
}
</script>
I'm not sure why the function is not working. I know for a fact that removeonecredit.php is doing its job.
turns out the = in the function arguments is a problem, I posted a question on how to escape it here: Javascript escape a equal sign PHP
trying to insert a record in my DB using AJAX for the very first time. I have the following...
Form
<form>
<input type="text" id="salary" name="salary">
<input type="button" onclick="insertSalary()">
</form>
AJAX
<script type="text/javascript">
function insertSalary()
{
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('current-salary').innerHTML=xmlhttp.responseText;
}
};
xmlhttp.open("POST","insert_salary.php",true);
xmlhttp.send("salary=" + document.getElementById("salary").value);
}
</script>
PHP
$uid = $_SESSION['oauth_id'];
$monthly_income = mysql_real_escape_string($_POST['salary']);
#Insert a new Record
$query = mysql_query("INSERT INTO `income` (user_id, monthly_income) VALUES ($uid, '$monthly_income')") or die(mysql_error());
$result = mysql_fetch_array($query);
return $result;
Nowmy data is being inserted into the table BESIDES the 'salary' which is being inputted as '0'
Once inserted I also have a div 'current-salary' that should then be populated with there inputted value only it isnt, Can anybody help me to understand where im going wrong?
If you want to save your self a lot of time, effort, and heartache, use the jquery library for your ajax requests. You can download it at http://jquery.com/
After adding a reference(Script tag) to the jquery script your javascript for the ajax request would become:
function insertSalary()
{
var salary = $("#salary").val();
$.post('insert_salary.php', {salary: salary}, function(data)
{
$("#current-salary").html(data);
});
}
Also keep in mind that using "insert_salary.php" as the url means it is a relative path and must be in the folder of the current running script.
Your php script needs to echo whatever you would like to be injected into your current-salary tag also.
I am using AJAX for the first time with PHP and mySQL.
I have been following some examples and tutorials on how to use AJAX with INPUT fields.
I have a form with 2 input fields.
1st input field: User type in 4 digits
2nd input field: The 4 digits are looked up in the mySQL db and outputted on this field.
Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.
How is this possible?
My js-file:
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","include/postalcode.php?q="+str,true);
xmlhttp.send();
}
You have written :
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
Instead of innetHTML use value:
document.getElementById("txtHint").value=xmlhttp.responseText;
Most examples are based on output in div's and span's while i am interested in output on my 2nd INPUT field.
You should be able to follow the examples with two changes:
use .value instead of .innerHTML
for input fields
look up your second input field by
its ID
document.getElementById("secondFieldId").value=xmlhttp.responseText;