Alright, I have looked around for this problem, but I can only find references to JSON, which I am currently not using. The array value that has a number in it passes, and the DIV updates. However, when ever I try to pass in a string, nothing happens. Here is the code:
<php>
$cont = array();
$cont[] = 'yo';
$cont[] = '2';
foreach($cont as $c){
$statement .= '<button type=\"button\" onclick=\"nFunc('.$c.')\">'.$c.'</button>';
}
</php>
<script>
function nFunc(str)
{
var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","test.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("p="+str);
}
</script>
<div id="myDiv">Default</div>
{$statement}
Please note I am doing testing with AJAX via IP.Board/IP.Content, so the tags and variables in {} are parsed by the IP.C engine.
This code outputs two buttons labeled "yo" and "2". When "2" is clicked, the DIV updates correctly. When "yo" is clicked, nothing occurs.
The test.php file is very simple:
<?php
$hello = $_POST['p'];
echo $hello;
?>
Thanks for any help beforehand.
This output HTML from your PHP Statement is invalid:
<button type=\"button\" onclick=\"nFunc(yo)\">yo</button><button type=\"button\" onclick=\"nFunc(2)\">2</button>
Notice onclick=\"nFunc(yo)\">
See your string parameter is not enclosed in quotes and also those backslashes are not needed there. That is why you see that error, which of course doesn't happen in case of a number.
$statement .= '<button type=\"button\" onclick=\"nFunc('.$c.')\">'.$c.'</button>';
should be
$statement .= '<button type="button" onclick="nFunc(\''.$c.'\')">'.$c.'</button>';
It works like a charm after that fix, I just tested.
Related
i have following set of table rows which i have fetched from the database
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(this.value);'>$id</td>";
this is actually a set of boxes and $id = $row['id'] is the number attached to each box. I want the mechanism like when a user clicks on the box the number attached to it should be echoed on a particular place on the webpage. MySql query and everything else works fine i have a problem with ajax code understanding i think so.
<script>
function valueAdd(str)
{
if (str=="")
{
document.getElementById("seats").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("seats").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ceckbus.php?q="+str,true);
xmlhttp.send();
}
</script>
waiting for ur help everyone!!
First I suggest that you use a javascript library in order to use Ajax, and avoid trying to fix the compatibility issues at your own. For example you could use jquery.com, it is pretty standard nowadays.
For the question part, please have a look at your echo code, there is an error at 'addValue(this.value);'
As I can understand the question, you want on the click event to have the $id. A simple fix it would be by using the innerHTML property, instead on .value:
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(this.innerHTML);'>$id</td>";
or better on the echo you could pass the value directly to the addValue function, eg:
echo "<td value='".$id."' style='border:double' bgcolor='" . getcolour($catc) . "' onclick = 'addValue(\"" . $id . "\");'>$id</td>";
I am a noob and i am trying to get this script working. It works perfectly in all browsers except IE. I have tried most of the solutions here on stackoverflow but none of them works for me. I am doing some kiddish mistake somewhere which I am unable to spot. Would request your help.
Also just to let you know the DOM Objects format of using appendchild might not work 100% correctly for me since this script basically passes values from a drop down which shows say numbers from 1 to 10 and based on those numbers it pulls up corresponding charts. So if I am viewing chart 1 then i can go ahead and select the drop down to view chart 3 as well. But if we use the appendchild property, it only allows me to see one of the charts by selecting 1 to 10 and it doesn't work unless I refresh the page. So unless deletechild is used I think it would not work, just my guess. Hence, I have commented that section out and I am using the good old method of innerhtml.
This is my JS function.
function showUser(str) {
debugger;
var xmlhttp = GetXmlHttpObject("Browser does not support HTTP Request");
if (str=="") {
document.getElementById("pairname").innerHTML="";
return;
}
/* <-- I HAVE ALREADY COMMENTED THIS OUT & am using a more robust function for this.
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("pairname").innerHTML=xmlhttp.responseText;
//AS YOU CAN SEE THE ALTERNATIVE SOLUTION OF USING DOM OBJECTS HAS BEEN COMMENTED OUT.. SINCE THIS IS ALSO NOT WORKING IN IE FOR ME.
//var wrappingElement = document.createElement("div");
//wrappingElement.innerHTML = xmlhttp.responseText;
//document.getElementById('pairname').appendChild(wrappingElement);
}
}
xmlhttp.open("GET","getimgdetails.php?q="+str,true);
xmlhttp.send();
}
function GetXmlHttpObject(errorMessage) {
var r = false;
try { r = new XMLHttpRequest(); }// Opera 8.0+, Firefox, Safari
catch(e) {
// Internet Explorer Browsers
try { r = new ActiveXObject("Msxml2.XMLHTTP"); }// older IE
catch(e) {
try { r = new ActiveXObject("Microsoft.XMLHTTP"); }// IE 5+
catch(e) {// AJAX not possible
if(errorMessage) { alert(errorMessage); }
}
}
}
return r;
}
AND THIS IS WHERE I AM SHOWING IT
<div id="pairname"><b>Charts will be shown here.</b></div>*
AND this is the Script which receives and sends back the info. SO basically it shows 5 charts for each number 1 to 10.
$q=$_GET["q"];
Echo "<img src=mqluploads/".$q."15.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."60.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."240.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."1440.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."10080.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
Echo "<img src=mqluploads/".$q."43200.gif?".time()."></img>";
Echo "<br>";
Echo "---------------------------------------------------------";
Echo "<br>";
?>
Many many thanks in advance for your help and for reading this far. I have been struggling with this for the past one week and now finally decided to seek some help. Looking forward to your advice.
Have you checked that your php script is returning some data. Try to access getimgdetails.php?q=str (replace str with correct paremeter) directly in browser.
Also place an alert(xmlhttp.responseText) before this line:
document.getElementById("pairname").innerHTML=xmlhttp.responseText;
This will help you to debug.
I had xmlHTTPrequest GET script which was working fine, but because of server issues I had to change it to POST method. I am unable to get the data in $_POST variable. When I checked in CHROME INSPECTOR debug tool, GET Method status is 200 ok. Need help to see if the javascript is correct.
xmlHTTPrequest file:
<script type="text/javascript">
function showprodes(str2)
{
var q2 = encodeURIComponent(str2);
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var url = "http://www.amg.in/amogtst/rateprod.php";
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send(q2);
}
</script>
<?
$result2 = mysql_query("SELECT Prod_desc FROM PRODMAST ORDER BY Prod_desc");
echo "<form name='f1'>";
echo " <span class='style3'>Gas Type </span> <select name='Proddesc' onchange=\"showprodes(this.value);\"><option value=0>Select a Product</option>";
while($nt2=mysql_fetch_assoc($result2))
{
echo "<option value='$nt2[Prod_desc]'>$nt2[Prod_desc]</option>";
}
echo "</select>";// Closing of list box
echo "</form>";
?>
Second script which updates the table as per the user selection from first php: rateprod.php file:
<?php
$q=$_POST['q2'];
$q2=mysql_real_escape_string($q);
include_once 'db.php';
mysql_query("UPDATE RATEMASTER_draft SET Prod_desc='$q2'");
?>
From looking at your AJAX code, you aren't supplying the POST variables correctly. The format for the POST string being given to xmlhttp.send() needs to be in the same format as a GET string. Trying using xmlhttp.send("q2=" + q2).
BTW, for future reference, you can use print_r($_POST) to show the contents of all POST variables. This can be very handy for debugging.
I want to display a tree with information about the Catalogue of Life which includes the Kingdom, phylum, order, etc information. I'm using ajax/php to interact with a mySQL database, while using javascript to display and output the information. I have finally completed the things I wanted it to do: expand and collapse on clicking, load only once, etc. But everytime something is clicked below the scroll limit, it bounces back to the top of the page. I researched this and saw something about including 'return false' in the function or the tag, but this did not solve the problem. Am I just putting it in the wrong place? Thanks for your help...
Here is the code: in its php/javascript goodness...
<?php
include ('includes/functions.php');
$connection=connectdb();
$result=runquery('
SELECT scientific_name_element.name_element as shortname ,taxon_name_element.taxon_id as taxonid
FROM taxon_name_element
LEFT JOIN scientific_name_element ON taxon_name_element.scientific_name_element_id = scientific_name_element.id
LEFT JOIN taxon ON taxon_name_element.taxon_id = taxon.id
LEFT JOIN taxonomic_rank ON taxonomic_rank.id = taxon.taxonomic_rank_id
LEFT JOIN taxon_name_element AS tne ON taxon_name_element.parent_id = tne.taxon_id
LEFT JOIN scientific_name_element AS sne ON sne.id = tne.scientific_name_element_id
LEFT JOIN taxon AS tax ON tax.id = tne.taxon_id
LEFT JOIN taxonomic_rank AS tr ON tr.id = tax.taxonomic_rank_id
WHERE taxon_name_element.parent_id is NULL');
set_time_limit(0);
ini_set('max_execution_time',0);
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
function load_children(id){
if(document.getElementById(id).active != 'true'){
if(document.getElementById(id).loaded != 'true'){
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()
{//actual stuff
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{//change the text
this.active='true'
//document.getElementById(id).innerHTML = xmlhttp.responseText;
var oNewNode = document.createElement("ul");
document.getElementById(id).appendChild(oNewNode);
document.getElementById(id).active = 'true';
document.getElementById(id).loaded = 'true';
oNewNode.innerHTML="<i>"+xmlhttp.responseText+document.getElementById(id).active+"</i>";
}
}
xmlhttp.open("GET","new.php?id="+id,true);
xmlhttp.send(false);
}else{
$("#"+id).children().children().show(); document.getElementById(id).active = 'true'}}else{ $("#"+id).children().children().hide();
document.getElementById(id).active = 'false';
}return false;}
</script>
</head>
<body>
<?php
echo "<ul>";
while($taxon_name_element = mysql_fetch_array($result)){
echo "
<li id=\"{$taxon_name_element['taxonid']}\" style=\"display:block;\">{$taxon_name_element['shortname']}</li>
";}
echo "</ul></body></html>";
?>
You were missing a closing parenthesis - if popped it in with a comment where I think you were wanting it. If JS has an error it generally won't execute, and instead it'll fall back to the non JS (ie, the return false wont fire because of the error). Were you getting an actual error when running this? The console is a good friend when you're debugging JS.
Formatting your code like I have below will also help to identify any missing elements more easily.
function load_children(id)
{
if(document.getElementById(id).active != 'true')
{
if(document.getElementById(id).loaded != 'true')
{
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()
{
//actual stuff
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//change the text
this.active='true'
//document.getElementById(id).innerHTML = xmlhttp.responseText;
var oNewNode = document.createElement("ul");
document.getElementById(id).appendChild(oNewNode);
document.getElementById(id).active = 'true';
document.getElementById(id).loaded = 'true';
oNewNode.innerHTML="<i>"+xmlhttp.responseText+document.getElementById(id).active+"</i>";
}
}
xmlhttp.open("GET","new.php?id="+id,true);
xmlhttp.send(false);
} // This was missing - were you getting an error?
}
else
{
$("#"+id).children().children().show(); document.getElementById(id).active = 'true'}
}
else
{
$("#"+id).children().children().hide();
document.getElementById(id).active = 'false';
}
return false;
}
When you're done checking and if everything is ok, take a look at: http://api.jquery.com/jQuery.get/ - the example's are at the bottom.
I hava set up AJAX script that when you click the button it calls a PHP document.
The problem I have is now some of the variables of the PHP file are set to constant, and now I want them to be conditional to what gets sent when the button is clicked.
I want there to be a check box, that if clicked, will set a PHP variable to be TRUE, while if not click, is set to be FALSE.
I hope the problem is clear enough.
I will now paste the code below because it may help to clarify the problem.
Sorry I have to paste this "huge" chunk of code but I´m not sure which may be useful and which not.
Here is index.html
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","roulette.php",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Welcome to Trufa’s Roulette</h2></div>
<button type="button" onclick="loadXMLDoc()">Spin the Wheel!</button>
</body>
</html>
And here is part of roulette.php
### Comprare pick to result
//The squares
$sqr_00 = -1;
$sqr_0 = 0;
$sqr_1 = 1;
//Placed bets, if true he placed bet
$bet_straight_placed = array(
$sqr_00 => false,
$sqr_0 => true,
$sqr_1 => true
);
What I would need as I told before, is to replace the FALSE in $sqr_00 => false, for a variable that is true when the checkbox is checked!
I hope the question is clear enough to be understood but PLEASE ask for any clarifications needed!
Thanks in advance!
Trufa
EDIT:
Addressing #grossvogel reply I am posting what I understood of his answer becuse it is not working. Sorry and thank you very much!
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
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("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","roulette.php",true);
xmlhttp.send();
}
var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);
</script>
</head>
<body>
<div id="myDiv"><h2>Welcome to Trufa’s Roulette</h2></div>
<form name="myform">
<input type="checkbox" name="checkbox" value="checkbox" />00<br />
<button type="button" onclick="loadXMLDoc()">Spin the Wheel!</button>
</form>
</body>
</html>
and the php
//The squares
$sqr_00 = -1;
$sqr_0 = 0;
$sqr_1 = 1;
//Placed bets, if true he placed bet
$chb_00 = $_GET['boxchecked'];
$bet_straight_placed = array(
$sqr_00 => (bool) $chb_00,
$sqr_0 => true,
$sqr_1 => true
);
//tell the user what number he piecked
echo "You chose this numbers: " . join(", ", array_keys(array_filter($bet_straight_placed))) . '<br />' . '<br />';
I´m sorry I know this must be totally basic but I cant get my head around this AJAX "thing" yet.
Thank you!!!
in your javascript, you can do something like this
var isChecked = document.myform.checkbox.checked ? 1 : 0;
xmlhttp.open("GET","roulette.php?boxchecked=" + isChecked,true);
Then, in your php, you can read $_GET['boxchecked'], which will be 1 if the box was checked, 0 if it wasn't.
EDIT: Extending the idea to more than one field.
To do this, you'd add as many checkboxes as you want, say they're names are value1, value2, value3, .... Then you can do something like this:
var value1 = document.myform.value1.checked ? 1 : 0;
var value2 = document.myform.value2.checked ? 1 : 0;
var value3 = document.myform.value3.checked ? 1 : 0;
...
and
xmlhttp.open("GET","roulette.php?value1=" + value1 + "&value2=" + value2 + "&value3=" + value3 ...,true);
In php, read $_GET['value1'], $_GET['value2'], $_GET['value3']...
Once you get the concepts, you can write some functions to reduce the duplication, if you want.