Jquery textinput change function is not working - php

I just get the parameters from PHP GET method and use them using jQuery. There is no output when run the page.
<?php
if (isset($_GET['url'])){
$url = $_GET['url'];
$url = explode(" " , $url);
echo end($url);
exit;
}
?>
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val():
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
</script>
</head>
<body>
<input type="text" style="width:100%;height:20px;"/>
</body>
</html>
I'm new to designs and hope mistake is there.

First of all put your document in standards mode (by using a proper doctype at the beginning, which means HTML 4/XHTML 1 strict or HTML 5). Then you can use error console for debugging.
I found following error
Unexpected token: ':' on line 7,
The colon should be a semicolon.
var url = $(this).val():
And then, the actual reason why nothing is happening is because the input is non-existent when the script is invoked/cached. You need to execute it after the DOM has been constructed.
$(document).ready(function() {
// content
});
Final code.
$(document).ready(function() {
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val();
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
});

put it in $(document).ready() like this:
$(document).ready(function() {
$('input[type=text]').change(function (){
if ($(this).val() !== ''){
var url = $(this).val():
$.post('grab.php?url='+url+'', function (data){
window.open(data, 'Download', 'width=10,height=10');
$(this).html('');
});
}
});
});

I think using a .change event on a text input isn't advisable. Usually .blur and .focus are more appropriate.
$(document).ready(function() {
$('input[type=text]').blur(function(){
var currentInput = this;
if ($(currentInput ).val() != ''){
var url = $(currentInput ).val();
$.post('grab.php?url='+url, function(data){
window.open(data, 'Download', 'width=10,height=10');
$(currentInput).val('');
});
}
});
});

Related

jquery $.getScript().. How to get data from external js file into array

I try to put data into js file, through "jquery $.post" and "fwrite php", and get back that data into array. How to do that?
here's the html:
<!doctype html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
$(document).ready(function() {
$("#button").click(function() {
if ($("#nameput").val() != "") {
$.post("processing.php", {
putname: $("#nameput").val()
});
var arr = [$.getScript("talk.js")];
alert(arr[0]);
}
})
})
</script>
</head>
<body>
<input type="text" id="nameput" />
<button id="button">send AJAX req</button>
</body>
</html>
Here's the php, I name it "processing.php" :
<?php
$file = fopen("talk.js","a");
$text = $_POST["putname"];
fwrite($file,'"'.$text.'",');
fclose($file);
?>
And "talk.js" will look like this :
"a","b","c",
Why I can't put that data from "talk.js" into array at " var arr = [$.getScript("talk.js")]; " as in html file above?
Here's what I try after I read comments. I change the scirpt into this:
<script>
$(document).ready(function() {
$("#button").click(function() {
if ($("#nameput").val() != "") {
$.post("processing.php", {
putname: $("#nameput").val()
}, function() {
$.getScript("talk.js", function(data) {
var arr = data.split(",");
alert(arr[0]);
})
})
}
})
})
</script>
And php into this:
<?php
$file = fopen("talk.js","a");
$text = $_POST["putname"];
fwrite($file,$text);
fclose($file);
?>
But it still not work?
here's a simplified version of your button click to help you out:
$("#button").click(function() {
$.getScript("talk.js", function(data){
var arr = data.split(',');
alert(arr[0]);
});
});
If you log the output of $.getScript you will easily see why what you're trying doesn't work.
Using this method you will get the data returned from the script ("a","b","c"), but you'll need to split it on a comma into an array. Then you can reference whichever part of the array you want.
Note that the each element of the array will have quotations around them.

why getElementById dosn't work with <script>... tag but work with php echo '<script>

i have an select option on my page & to load syslogout.php according to selected option i wrote this code :
echo '<script type="text/javascript">$("#divSys").mouseup(function() {
var open = $(this).data("isopen");
if(open) {
window.location="sysLogout.php?sysid="+document.getElementById("divSys").value;
}
$(this).data("isopen", !open); });
</script>';
it's work fine. but this code :
<script type="text/javascript">$("#divSys").mouseup(function() {
var open = $(this).data("isopen");
if(open) {
window.location="sysLogout.php?sysid="+document.getElementById("divSys").value;
}
$(this).data("isopen", !open); });
</script>
it isn't work and return value for sysid is fix (1 or 2 for instance) for all option.
why?
Wrap your code in $(document).ready(). This should resolve your issue:
<script type="text/javascript">
$(document).ready(function () {
$("#divSys").mouseup(function() {
var open = $(this).data("isopen");
if(open) {
window.location="sysLogout.php?sysid="+document.getElementById("divSys").value;
}
$(this).data("isopen", !open);
});
});
</script>
Try this:
<script type="text/javascript">
$(document).ready(function () {
$("#divSys").mouseup(function() {
var open = $(this).data("isopen");
if(open) {
window.location="sysLogout.php?sysid="+$(this).val();
}
$(this).data("isopen", !open);
});
});
</script>

JQuery after AJAX GET

I have this AJAX GET:
function edit(str){
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && ajax.status==200)
{
document.getElementById("editaircraftdialog").innerHTML=ajax.responseText;
$("#editaircraftdialog").dialog('open');
$("#loadingdialog").dialog('close');
}
}
ajax.open("GET","./edit_aircraft.php?icao="+str,true);
ajax.send();
$("#loadingdialog").dialog('open');
}
The JQuery code in edit_aircraft.php does not work. Here's an example:
<script>
$(function() {
$("#insertaircraft")
.button()
.click(function(event) {
});
});
</script>
I have had the same problem in the past and I cannot fix it.
You can not load JavaScript with innerHTML. It does not get evaluated.
Use jQuery to your advantage
function edit(str){
var loading = $("#loadingdialog").dialog('open');
var aircraft = $("#editaircraftdialog");
aircraft.load("./edit_aircraft.php?icao="+str, function(){
loading.dialog('close');
aircraft.dialog('open');
});
}

Loading Javascript with AJAX

I have this neat little code:
<script type="text/javascript" language="javascript">
$(function(){
$(window).hashchange( function(){
window.scrollTo(0,0);
var hash = location.hash;
if (hash == "") {
hash="#main";
}
var newhash = !/\.php$/i.test(hash)? hash+".html": hash;
ajax_loadContent('m',newhash.slice(1,newhash.length));
$('.menu li a').each(function(){
var that = $(this);
that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'current' );
});
})
$(window).hashchange();
});
</script>
The only problem is that if I navigate to pages containing Javascript that code isn't being executed. How can I make it run the Javascript code as if the browser loaded the page normally?
It will not be execute as it comes as text. You have to use "eval" function on your response text

PHP Script with Arguements to AJAX Auto Refresh

I would like to create a PHP page which accepts an arguement like so:
http://localhost/page.php?topic=Foo
and then pulls data from an SQL Database where topic=Foo but then automatically checks for new data every 10 seconds and refreshes a DIV tag using Ajax. I've tried and nothing works. Any help?
EDIT: here is the code I've used:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
ext = <?php $_GET[feedtitle] ?>
$(document).ready(function() {
$("#responsecontainer").load("response.php?ext=" + ext);
var refreshId = setInterval(function() {
$("#responsecontainer").load('response.php?ext=' + ext);
}, 9000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>
<div id="responsecontainer">
</div>
</body>
EDIT: I can do the SQL bit, it's just the getting the arguement to the response.php im having issues with.
EDIT: I have new code, but its still not working:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
function gup( name )
{ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
var feed = gup('f');
$(document).ready(function() {
$("#responsecontainer").load("response.php?ext=" + feed);
var refreshId = setInterval(function() { $("#responsecontainer").load('response.php? ext=' + feed); }, 9000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>
<div id="responsecontainer">
</div>
</body>
So, you need to
Get escaped URL parameter
,
output the jquery $.post function's result data and then you just need to know
How to refresh page with jQuery Ajax? and do an
AJAX Div Retrieval every 60 seconds?
I hope that helps :)

Categories