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.
Related
I want to fetch data from a PHP File
<script type="text/javascript">
function showUsers(str) {
if (str=="") {
document.getElementById("zzips").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("zzips").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getstate.php"+str, true);
xmlhttp.send();
}
<select name="state_hid" required onchange="showUsers(this.value);">
<option>AL</option><option>AK</option>
<option>AZ</option><option>AR</option>
<option>CA</option>
<option>CO</option>
<option>CT</option><option>DE</option><option>FL</option>
<option>GA</option>
</select>
<select id="zzips" name="zzip">
</select>
PHP file
$sql=mysql_query("Select* From zipcod Where abb='".$isd."' ") or die('error');
while($row=mysql_fetch_array($sql, MYSQL_ASSOC))
{
$zip=$row['zip'];
echo '<option>'.$zip.'</option> ';
}
mysql_close( $sql );
?>
This Ajax is Working fine on Localhost but when i upload the same code on live server it is not working . Does anyone have any idea.
Please advice.
1)I think this
xmlhttp.open("GET", "getstate.php"+str, true);
should be
xmlhttp.open("GET", "fullpath/getstate.php?q="+str, true);
2)And there is no value in the option field
3)Try accessing directly in your browser
path_to_file/getstate.php?q='CA'
4) Check the file permission of getstate.php file
Please provide full absolute path from http://domain.com/filepath in your
xmlhttp.open("GET", "getstate.php"+str, true);
to
xmlhttp.open("GET", "http://path-to/getstate.php"+str, true);
replace path-to with your full path
Hope this helps..
I have a simple PHP page that sends a query to a mySQL database and displays the data onto a HTML table.
How can I get it to do this every 5 seconds? Currently it does this upon the page loading. The database will have changing data, so am trying to get the code to refresh every 5 seconds to keep the table updated.
How might I go upon doing this?
You need to use a client-side scripting language such as JavaScript to have a webpage do something after it has been served to a client.
Specifically, you will want to use timers and AJAX calls. You may also find the jQuery library useful for abstracting AJAX calls however it is not a requirement.
You need to use javascript ajax. For example ,
<script>
function loadXMLDoc()
{
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; // your div
}
}
xmlhttp.open("GET","getdatabase.php",true); //your php file
xmlhttp.send();
}
window.setInterval(function(){
loadXMLDoc();
}, 5000);
</script>
Then in html , you must have a div with specified ID(myDiv) Fpr example:
<body>
<div id="myDiv"></div>
</body>
Then in getdatabase.php you must fetch the value from the database and echo it out. For example,
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Hope it works , see more at www.w3schools.com/
Use javascript timers. From there-on u can periodically run the SQL command to read the databases.
If this problem still arises, I can post some example code.
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.
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.
I have this issue, in FF it works great but in IE only works once...
the html is
<form>
<input type="button" value="test" onclick="javascript:vote();"/>
</form>
the javascript
<script type="text/javascript">
function vote(){
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","../php/votes.php",true);
xmlhttp.send(null);
}
</script>
and the PHP code is only a update
<?php
$con = mysql_connect("localhost","mylog","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('versus',$con);
mysql_query("update picture_vs set votes = votes + 1");
?>
any idea?
Modify the following line to...
xmlhttp.open( "GET",
"../php/votes.php?random=" +
Math.random(), true);
This will prevent IE from caching your request by URI.