index.php
<head>
<script>
function getpage(pageName) {
var obj = new ActiveXObject("msxml2.xmlhttp");
obj.open("GET", pageName);
obj.send('A=1&B=2&C=3');
var txt = obj.responseText;
myText.value += txt;
}
</script>
</head>
<body>
<input type="text" id="myText"/>
<input type="button" onclick='getpage("http://localhost/Last/callPageForIE/info.php")';/>
</body>
</html>
info.php
<?php
$A = $_GET['A'];
$B = $_GET['B'];
$C = $_GET['C'];
$sum = $A + $B + $C;
echo "your sumuatsdion is ".$sum;
?>
tried to get the result from info.php but it always give me zero I don't know why, can someone tell me where is my wrong?
You are passing your data as the request body. This is what you do for POST requests, not GET requests. GET request data needs to be encoded in a query string in the URL.
obj.open("GET", pageName + '?A=1&B=2&C=3');
obj.send();
PHP is then casting the undefined variables to 0.
You are also using the obsolete, Microsoft only, ActiveX implementation of XHR. You should switch to the standard, cross-browser implementation instead.
Related
I use the code below for simple search and it works well in index.php but because i added the code in header.html for include in all pages and the code result work only in index.php.
https://stackoverflow.com/a/34131517/5227465
index.php?text=keyword = ok
otherpage.php?text=keyword = here not work because search Processing only in index.php
I think the problem in this element means the current page that contains the code:
(document.getElementById)
<form id = "your_form" onsubmit="yourFunction()">
<input type="text" name="keywords">
<input type="submit" value="Search">
</form>
function yourFunction(){
var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}
any help?
I didnt test it but try this:
function yourFunction(){
var action_src = "http://localhost/test/<?php echo basename(__FILE__, ''); ?>" + document.getElementsByName("keywords")[0].value;
var your_form = document.getElementById('your_form');
your_form.action = action_src ;
}
My PHP code is:
<?php
class Sample{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) ){
echo "Starting to read ";
$req = $_GET[ 'request' ];
$result = json_decode($req);
if( $result->request == "Sample" ){
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}else{
echo "Not Supported";
}
}
?>
Is there anything wrong
I want to send a JSON to this php and read the JSON that it returns using java script , I can't figure out how to use JavaScript in this , because php creates an html file how Can I use $_getJson and functions like that to make this happen ?!
I tried using
$.getJSON('server.php',request={'request': 'Sample'}) )
but php can't read this input or it's wrong somehow
thank you
try this out. It uses jQuery to load contents output from a server URL
<!DOCTYPE html>
<html>
<head>
<title>AJAX Load Test</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function(event) {
$('#responce').load('php_code.php?request={"request":"Sample"}');
});
});
</script>
</head>
<body>
<p>Click on the button to load results from php_code.php:</p>
<div id="responce" style="background-color:yellow;padding:5px 15px">
Waiting...
</div>
<input type="button" id="button" value="Load Data" />
</body>
</html>
Code below is an amended version of your code. Store in a file called php_code.php, store in the same directory as the above and test away.
<?php
class Sample
{
public $name = "N3mo";
public $answer = "";
}
if( isset( $_GET['request'] ) )
{
echo "Starting to read ";
$req = $_GET['request'];
$result = json_decode($req);
if( isset($result->request) && $result->request == "Sample" )
{
$ans = new Sample();
$ans->answer = " It Is Working !!! ";
echo json_encode($ans);
}
else
{
echo "Not Supported";
}
}
Let me know how you get on
It would be as simple as:
$.getJSON('/path/to/php/server.php',
{request: JSON.stringify({request: 'Sample'})}).done(function (data) {
console.log(data);
});
You can either include this in <script> tags or in an included JavaScript file to use whenever you need it.
You're on the right path; PHP outputs a result and you use AJAX to get that result. When you view it in a browser, it'll naturally show you an HTML result due to your browser's interpretation of the JSON data.
To get that data into JavaScript, use jQuery.get():
$.get('output.html', function(data) {
var importedData = data;
console.log('Shiny daya: ' + importedData);
});
When sending the request from the jQuery Mobile script to the specified PHP file, nothing is returned, nothing is appended to the html file. Here's the URL of the page:
localhost/basket/newstext.html?url=http://www.basket-planet.com/ru/news/9235
newstext.html:
<head>
<script src="js/newstext.js"></script>
</head>
<body>
<div data-role="page" id="newstext">
<div data-role="content">
<div id="textcontent"></div>
</div>
</div>
</body>
newstext.js:
var serviceURL = "http://localhost/basket/services/";
$('#newstext').bind('pageshow', function(event) {
var url = getUrlVars()["url"];
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText);
});
function displayNewsText(data){
var newstext = data.item;
console.log(newstext);
$('#textcontent').text(newstext);
$('#textcontent').trigger('create');
}
function getUrlVars(){
//it displays in the alert perfectly, shortening the message here
}
getnewstext.php:
<?php
include_once ('simple_html_dom.php');
$url = $_GET['url'];
$html = file_get_html(''.$url.'');
$article = $html->find('div[class=newsItem]');
$a = str_get_html(implode("\n", (array)$article));
//parse the article
header("Content-type: application/json");
echo '{"item":'. json_encode($a) .'}';
?>
I think my problem is how I'm encoding the $a variable in the PHP script. The $a variable contains html tags of all kind...how can I append it in the html file?
Where you have this line:
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText);
Change it to be:
$.getJSON(serviceURL + 'getnewstext.php?url='+url, displayNewsText, function(response){
$('#elem').append(response);
});
Where #elem is the name of the element that you want to append the data, returned from the PHP file, to.
i'm trying to create a simple autocomplete textbox that takes the suggestions from an array.the code i'm using(based on this) is :
call.php
<?php
$list = array(
"Autocomplete",
"Metapher",
"Metatag");
for($i=0; $i<count($list); $i++){
if(strpos($list[$i], $_GET['str']) !== FALSE && strlen($_GET['str']) >= 2){
echo str_ireplace($_GET['str'], '<b style="color: red;">'.$_GET['str'].'</b>', $list[$i]) . '<br>';
}
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>AJAX - 03</title>
<script type="text/javascript">
var ajax = new XMLHttpRequest;
function t(){
ajax.open("GET", "call.php?str=" + document.getElementById("test").value, false);
ajax.send();
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && ajax.status==200)
{
document.getElementById("container").innerHTML = ajax.responseText;
}
}
}
</script>
</head>
<body>
<div id="container" style="border: 3px; border-style: solid; font-size: 42pt; border-radius: 7px;">
Text
</div>
<br><br>
<input id="test" type="text" onkeyup="javascript:t()">
</body>
</html>
but nothing comes up at the suggestion box.I can't find any syntax errors so i suppose there is something wrong with the logic?
UPDATE:
after the advice from PLB and FAngel i added the onreadystatechange and the checks.However it still doesn;t work properly.Actually i just found that if you type a compination of letters that are inside one of the 3 words the suggestions come up properly.It just doesnt work if you type the starting letters of a word.For example if i give "com" as input the word Autocomplete comes up.However if i give "Aut" then nothing.So i guess the actual problem is here:
if(**strpos($list[$i], $_GET['str']) !== FALSE** && strlen($_GET['str']) >= 2)
From what i read here http://php.net/manual/en/function.strpos.php the problem could be the use of != but i use !== as i should.Any thoughts?
You are missing that your request is asynchronous. So when you run this line: document.getElementById("container").innerHTML = ajax.responseText;, request is not done yet. Take a look at this . onreadystatechange is what you need. Or make that call synchronous
You can also work it like this.
http://jsfiddle.net/qz29K/
All you need is you just replace the json array with php jsonencoding like this
$list = array(
"Autocomplete",
"Metapher",
"Metatag");
<script>
var availableTags = <?php echo json_encode($list) ?>
</script>
Hope this will help.
It totally WORKS!
I tried it.
I had input : Met
and it gave me Metaphor and one more word.
However, for advanced usage. Check this out, you gonna love it.
http://jqueryui.com/demos/autocomplete/
Working Example:
This is almost identical to code I use in another places on my page but fails here for some reason.
<?php
//$p = "test";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = "test";
</script>
Failing example: (alert still works)
<?php
$p = "test of the var";
?>
<script>
alert('posts are firing? ');
parent.document.getElementById('posts').innerHTML = '<?php $p; ?>';
</script>
Try
'<?php echo $p; ?>';
or
'<?= $p ?>';
Debugging 101: Start checking all variable values.
alert(parent);
alert(parent.document);
alert(parent.document.getElementById('posts'));
as well as the value rendered by: '<?php $p; ?>'
Make sure your 'posts' object (I guess it is DIV or SPAN) loads before you fill it using javascript.
You're trying to generate javascript with php, here I use a simple echo:
<?php
$p = "test of the var";
echo"
<div id='posts'></div>
<script type='text/javascript'>
var posts = document.getElementById('posts');
posts.innerHTML = '$p';
</script>
";
?>
Note the $p and that the div is printed before the javascript!
You are not outputting the variable data is why it isn't working. You need to echo or print the variable $p.
In your example the $p is being evaluated, not printed.
To print it you should use print, echo, or the syntax <\?=$p;?>. without the \