I appreciate that this might be an exceedingly simple error, but I have looked over it several times and my eyes are bleeding and I have a deadline so I thought I'd post my problem here and see if anyone can help:
Essentially, I have two PHP pages; index.php and backend.php. Two pieces of data from index.php need to be sent to backend.php: url and filename. Filename is randomly generated on index.php (the method I've used isn't the most random method available and it's pretty clunky, but that doesn't matter for the purposes of this project), and url is collected from the user via a text input field. Filename corresponds to an xml file that will be created by backend.php, and used by backend.php to record its activities. This xml file will then be read using AJAX in index.php, and the activities of "backend.php" will be reported to the user. Before I continue explaining the problem, here is the code for index.php:
[CODE]
<html>
<head>
<title>Blah!</title>
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
a:
$filename = genfilename().".xml";
if(file_exists("./logs/".$filename)) goto a;
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//Use jQuery to send data via POST to backend.php
$(document).ready(function(){
$("submitbutton").onclick(function(){
url=$("url").val();
file="<?php echo $filename; ?>";
$.post("backend.php", {url:url , filename:file});
// Code to read XML file created by backend.php (currently incomplete)
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Print YEA! if it works
$("events").html("YEA!");
}
}
xmlhttp.open("GET","<?php echo "./logs/".$filename; ?>",true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h1>Welcome to blah</h1>
<p>Please complete the form below: </p>
<table>
<tr>
<td>URL:</td>
<td><input type="text" name="url" /></td>
<td><button name="submitbutton" type="button">Submit</button></td>
</tr>
</table>
<br/><br/><br/>
<span id="events"></span>
</body>
</html>
[/CODE]
So as you can see (providing I haven't made an enormous mistake somewhere due to lack of research - in which case I apologise for wasting your time), this file generates a random filename, gets a url from the user. It should then send these to backend.php, however this is not happening (using Firebug I was able to see that no such requests were being sent). This is resulting in the xml file not being created. I have used AJAX and jQuery extensively on this computer before with other projects, so it has to be something I've done wrong with this project specifically, I just can't quite figure it out.
As I said, this may be a silly error, but if someone could point it out to me I would be very grateful.
Rob
$("submitbutton").onclick(function(){
$("#submitbutton").onclick(function(){
The # marks an id.
$("events").html("YEA!"); Just needs # in front of events. To mark the html element with id = "events"
$(""events").html("YEA!");
<button name="submitbutton" type="button">Submit</button>
Would need an id="submitbutton"
Thanks for the reply, Freze. I've made the changes you've suggests, but it's still not working. I used Firebug and loaded the page with your changes in place, and when I click the submit button, the POST is still not being sent to backend.php
[QUOTE=Freze;33953213]$("submitbutton").onclick(function(){
$("#submitbutton").onclick(function(){
The # marks an id.
$("events").html("YEA!"); Just needs # in front of events. To mark the html element with id = "events"
$(""events").html("YEA!");
<button name="submitbutton" type="button">Submit</button>
Would need an id="submitbutton"[/QUOTE]
its not onclick its .click
Ok, changed it to .click, still no luck :/
also variables, you need to put var infront of them.. don't you?
Ok, tried that as well. The problem is that that block of code isn't even being executed. I have no idea what's going on with it.
can you give us the code you have so far if you updated any?
Sure, the updated code is:
[CODE]
<html>
<head>
<title>Blah!</title>
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
a:
$filename = genfilename().".xml";
if(file_exists("./logs/".$filename)) goto a;
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//Use jQuery to send data via POST to backend.php
$(document).ready(function(){
$("#submitbutton").click(function(){
url=$("url").val();
file="<?php echo $filename; ?>";
$.post("backend.php", {url:url , filename:file});
// Code to read XML file created by backend.php (currently incomplete)
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Print YEA! if it works
$("#events").html("YEA!");
}
}
xmlhttp.open("GET","<?php echo "./logs/".$filename; ?>",true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h1>Welcome to blah</h1>
<p>Please complete the form below: </p>
<table>
<tr>
<td>URL:</td>
<td><input type="text" name="url" /></td>
<td><button id="submitbutton" name="submitbutton" type="button">Submit</button></td>
</tr>
</table>
<br/><br/><br/>
<span id="events"></span>
</body>
</html>
[/CODE]
you dont have var before the variable
[editline]29th December 2011[/editline]
also its id="submitbutton" btw not name
Ok, I've made those changes. It's still not working :P
[QUOTE=DrSwitched;33954408]Sure, the updated code is:
[CODE]
url=$("url").val();
file="<?php echo $filename; ?>";
[/code]
[/quote]
those?
Woops, forgot about those the first time. Have made them now, but it still doesn't seem to be working. I'm so confused, I can't see anything wrong at all yet I must have made a mistake somewhere :/
its id="url" instead of name
and also its ("#url")
Ok, newly updated code:
[code]
<html>
<head>
<title>Blah!</title>
<?php
//Generate XML filename:
function genfilename()
{
$name = substr(base64_encode(rand(1000000000,9999999999)),0,10);
return($name);
}
a:
$filename = genfilename().".xml";
if(file_exists("./logs/".$filename)) goto a;
?>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//Use jQuery to send data via POST to backend.php
$(document).ready(function(){
$("#submitbutton").click(function(){
var url=$("#url").val();
var file="<?php echo $filename; ?>";
$.post("backend.php", {url:url , filename:file});
// Code to read XML file created by backend.php (currently incomplete)
var xmlhttp;
if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }
else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//Print YEA! if it works
$("#events").html("YEA!");
}
}
xmlhttp.open("GET","<?php echo "./logs/".$filename; ?>",true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<h1>Welcome to blah</h1>
<p>Please complete the form below: </p>
<table>
<tr>
<td>URL:</td>
<td><input type="text" name="url" /></td>
<td><button id="submitbutton" type="button">Submit</button></td>
</tr>
</table>
<br/><br/><br/>
<span id="events"></span>
</body>
</html>
[/code]
Still no luck.
<input type="text" name="url" /></td>
you need id="url"
Ok, I've done that. STILL no luck xD This is getting ridiculous haha, thank you for being so patient with this. Any other suggestions?
you need pren for the last to brackets
like });
[editline]29th December 2011[/editline]
the ones after xmlhttp.send();
YES! Finally, it works. Thank you so much for taking the time to help, I should have checked this sort of thing before I posted so sorry if I wasted your time.
Sorry, you need to Log In to post a reply to this thread.