call javascript function with parameters from php code - javascript

I want call a javascript function from php and execute a ajax call.My php code is:
echo '<script>change2('.$stand.'); </script>';
and my js code is :
function change2(c){
alert(c);
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","change2.php?c="+c,true);
xmlhttp.send();
}
The alert return undifined..

You can do this : <input type="hidden" value="<?php $stand ?>" id="someID">
and in your Js
<script>change2(document.getElementByID('someID'); </script>
I think it's working.

Related

Different value passed for cardnum begins with 0 in ajax

I notice as I'm trying to pass the value of cardnum, date and str to another page which is updatestaffshift page, and after echo the cardnum value, it appears to be a different value than the correct cardnum. However it only occur for cardnum begins with number 0. Numbers other than 0 the passing value is correct. I have included the output of my result. Please advice on what is wrong with this. Thank you.
function updatestaffshift(cardnum,date,str)
{
var xmlhttp;
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","updatedata/updatestaffshift.php?u="+str+"&cardnum"+cardnum+"&date="+date,true);
xmlhttp.send();
Echo card no
$sql=" SELECT * FROM staff_detail";
$query = mysqli_query($sql);
$int = 0;
while ($rr = mysqli_fetch_assoc($query)){
$cardnum[$int] = $rr['cardnum'];
echo "Correct Card No :" ; echo $cardnum[$int];
updatestaffshift.php - Echo passed value
echo 'Passed Value of Card No : '; echo $cardnum = $_GET['cardnum'];
Output of result

How do I get a parameter value using XMLHttpRequest?

I have defined the following function in JavaScript for calling another file named database_value.php on which I am passing Var str:
function subFunction(str){
if (str=="") {
document.getElementById("sci").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("sci").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","database_value.php?q="+str,true);
xmlhttp.send();
}
How can I get the value of str on the file database_value.php?
database_value.php
<?php
if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['q'] ) ){
/* use the value from the GET array */
$q=$_GET['q'];
/* or better with some filtering for user supplied values */
$q=filter_input( INPUT_GET, 'q', FILTER_SANITIZE_STRING );
/* use the param in your query */
/* echo results from db query or just the value as a test */
echo $q;
}
?>

How to use the response PHP variables in a XHR call?

I have two pages say, page1.php and page2.php. In page1.php I have a button, when clicked it makes XHR call to page2.php and shows the response in a defined divison i.e. "print" in page1.
Code for page1.php
<html>
<button type="button" onclick="randomFunction()">Request data</button>
<div id="print"></div>
<script type="text/javascript">
function randomFunction()
{
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("print").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","page2.php",true);
xmlhttp.send();
}
</script>
</html>
Code for page2.php
<?php
$a = "apple";
$b = "banana";
echo $a;
echo $b;
?>
Output I am getting now, http://imgur.com/tyqEOgW
I want to manipulate with the response I'm getting from page2. As in I want to show "apple" in red and "banana" in blue on page1.php
How do I do that?
Send back JSON from page2.php and then add your custom HTML with that data to page1.php.
page1.php
<html>
<button type="button" onclick="randomFunction()">Request data</button>
<div id="print"></div>
<script type="text/javascript">
function randomFunction()
{
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)
{
var data = JSON.parse(xmlhttp.responseText);
var html = '<span class="apple">'+data.a+'</span><span class="banana">'+data.b+'</span>';
document.getElementById("print").innerHTML = html;
}
}
xmlhttp.open("POST","page2.php",true);
xmlhttp.send();
}
</script>
</html>
page2.php
<?php
$a = "apple";
$b = "banana";
echo json_encode( array( 'a' => $a, 'b' => $b ) );
?>
Now you could style these spans like you want. Of course you can edit the HTML structure to your desire.
Only PHP
page1.php
<html>
Request data
<div id="print">
<?php
// Show data only, if our link was clicked
if( $_GET['showData'] == 1 ){
// Get page2.php
require_once('page2.php');
echo '<span class="apple">'.$a.'</span><span class="banana">'.$b.'</span>';
}
?>
</div>
</html>
page2.php
<?php
$a = "apple";
$b = "banana";
?>

Disallowed Key Characters Error in my code

Here is the code which give that error.
<html>
<head>
<script type="text/javascript">
var d = new Date();
var date = d.toLocaleString();
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("GET","test.php?date"+date,true);
xmlhttp.send();
</script>
</head>
<body>
<div id ="myDiv"></div>
</body>
</html>
Here is php code.
<?php
$date = $_GET['date'];
echo $date;
?>
The error is the querystring
xmlhttp.open("GET","test.php?date"+date,true);
^^^^
It missing the = between the name and the value, Add the = and the server will stop complaining that it does not know what date[DateString] is in the GET parameters
xmlhttp.open("GET","test.php?date="+date,true);
^
better yet to encode it
xmlhttp.open("GET","test.php?date="+encodeURIComponent(date),true);
^
replace this line and try again
xmlhttp.open("GET","test.php?date="+date,true);

AJAX Response Text Empty

I'm creating a system that interacts with Minecraft server using AJAX. My JavaScript code is as follows:
function doMessage(name, message)
{
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.open("GET","http://duncan.usr.sh/omnicraft/api/broadcast-msg.php?message=" + message + "&user=" + name,false);
xmlhttp.send();
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
My PHP code is as follows:
<?php
header('content-type: text/plain');
echo "OmniCraft/Api/RequestHTTP";
if($_GET['message'] == "" or ctype_space($_GET['message']) or $_GET['user'] == "" or ctype_space($_GET['user']))
{
echo "\nOmniCraft/Api/Request/Failed";
echo "\nRequest invalid: No message specified";
exit;
}
include_once '../MinecraftQuery.class.php';
$Query = new MinecraftQuery( );
try
{
$Query->Connect( 'localhost', 25565 );
$info = $Query->GetInfo( );
}
catch( MinecraftQueryException $e )
{
echo "\nOmniCraft/Api/Request/Failed";
echo "\nServer not online";
exit;
}
include_once("rcon.class.php"); //Include this file
$r = new rcon("127.0.0.1",25575,"notRealPasswordHere"); //create rcon object for server on the rcon port with a specific password
if($r->Auth()) //Connect and attempt to authenticate
{
$message = $_GET['message'];
$user = $_GET['user'];
$r->rconCommand("/tellraw #a {'text':'<$user using OmniMessage> $message','color':'white'}"); //send a command
echo "\nOmniCraft/Api/Request/Success";
echo "\nMessage sent successfully!";
}
else
{
echo "\nOmniCraft/Api/Request/Failed";
echo "\nUnable to authenticate to RCON";
exit;
}
?>
My form is as follows:
<form >
<div class="input">
<input type="text" placeholder="Message" style="padding: 20px 28px;font-size: 25px;" required="true" name="message">
</div>
<div class="input">
<input type="text" placeholder="Name" style="padding: 20px 28px;font-size: 25px;" required="true" name="name">
</div>
<button class="btn btn-large btn-success" type="button" onclick="doMessage(this.form.name.value, this.form.message.value)">Send</button>
<div>
<p class="lead" id="preview">< using OmniMessage></p>
<div>
<div id="response">
</div>
The AJAX query executes well, except for the fact that the responseText variable seems to not contain the response text (i.e. the DIV with the id 'response' remains empty after the query is complete).
The correct contents of the response DIV should be:
OmniCraft/Api/RequestHTTP
OmniCraft/Api/Request/Success
Message sent successfully!
However, this is not the case. What am I doing wrong?
EDIT: More Info:
I get the following error:
[20:14:21.855] NS_ERROR_FAILURE: Failure # http://omnicraft.duncan.usr.sh/omnimessage/:154
Line 154 is: xmlhttp.send();
(or maybe document.getElementById("response").innerHTML=xmlhttp.responseText;
Try this
You haven't handled onreadystatechange
function doMessage(name, message) {
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.open("GET","http://duncan.usr.sh/omnicraft/api/broadcast-msg.php?message=" + message + "&user=" + name,false);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("response").innerHTML=xmlhttp.responseText;
}
}
}
Try removing the header call in your PHP code. I’m having a very similar issue that only goes away when I no longer issue a Content-Type header through PHP.

Categories