Nexy Posted June 24, 2008 Share Posted June 24, 2008 Why Hello There! For some reason the ajax function below is not being called from my submit button: JS File: function sendRequest(mode) { alert("function called"); $('shoutbox').innerHTML= "Processing..."; if (mode == "shout") new Ajax.Request("process.php?mode=shout", { method: 'post', postBody: 'text='+ $F('shoutsub'), onComplete: showResponse }); } function showResponse(req) { $('shoutbox').innerHTML = req.responseText; } I put "alert("function called");", to see if it was actually getting there, but no alert comes up. Form: <?php echo "<form method='post' id='form' name='form' onsubmit='return false;'> <fieldset id='shout'> <label for='shoutext'>Message:</label> <input type='text' id='shoutext' name='shoutext' tabindex='6' style='width: 360px' /> <input type='submit' id='shoutsub' name='shoutsub' value='Shout!' onclick='onclick='sendRequest(\'shout\')' tabindex='7' /> </fieldset> </form>"; ?> When I click submit, nothing happens. I have the js file included in my head tag with "<script src=". Any help would be appreciated. Thank You! Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 24, 2008 Share Posted June 24, 2008 you have two onclicks, try this: <input type='submit' id='shoutsub' name='shoutsub' value='Shout!' onclick='sendRequest(\'shout\')' tabindex='7' /> Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 24, 2008 Author Share Posted June 24, 2008 Oh that was a typo while typing the code here. It still does nothing. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 24, 2008 Share Posted June 24, 2008 JavaScript doesn't like the \' use this: <input type='submit' id='shoutsub' name='shoutsub' value='Shout!' onclick='sendRequest(\"shout\")' tabindex='7' /> or <input type='submit' id='shoutsub' name='shoutsub' value='Shout!' onclick=\"sendRequest('shout')\" tabindex='7' /> or even better, just stop the PHP and start it back up again: <?php //php code here ?> <form method="post" id="form" name="form" onsubmit="return false;"> <fieldset id="shout"> <label for="shoutext">Message:</label> <input type="text" id="shoutext" name="shoutext" tabindex="6" style="width: 360px" /> <input type="submit" id="shoutsub" name="shoutsub" value="Shout!" onclick="sendRequest('shout')" tabindex="7" /> </fieldset> </form> <?php //more php code ?> Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 24, 2008 Author Share Posted June 24, 2008 I did all 3 ways, and it still does nothing: if($_SESSION['username'] || $_COOKIE['user']) { echo "<div id='shoutform' style='display: none'>"; if($_POST['shoutsub'] && $sherror == "1") { echo "<span style='color: #CD5C5C; font-size: .8em'>Please enter a message!</span>"; } if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) { echo "<div style='margin-top: .8em; margin-bottom: 0' />"; } ?> <form method="post" id="form" name="form" onsubmit="return false;"> <fieldset id="shout"> <label for="shoutext">Message:</label> <input type="text" id="shoutext" name="shoutext" tabindex="6" style="width: 360px" /> <input type="submit" id="shoutsub" name="shoutsub" value="Shout!" onclick="sendRequest('shout')" tabindex="7" /> </fieldset> </form> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 24, 2008 Share Posted June 24, 2008 if you comment everything out inside sendRequest except for the alert(), does the alert then work? also: -Which JS library are you using...prototype? -Are you getting any JavaScript errors? -What browser are you using? -Do you have a link to the page that I can load up and see what happens? Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 24, 2008 Author Share Posted June 24, 2008 No, the alert does not come up if I comment everything out except for alert(). -Yes, I'm using prototype. -No errors, whatsoever. -Firefox 3, IE7 -Yes: http://divnx.net It's the shoutbox, you can login with Divnx/testtest If you hit shout!, nothing happens. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 24, 2008 Share Posted June 24, 2008 In FF2 and FF3 I get an illegal character error on this line: illegal character ���$('shoutbox').innerHTML= "Processing...";\n Try retyping those spaces Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 25, 2008 Author Share Posted June 25, 2008 I rewrote it and it still doesn't work. BTW, how did you get that error? I don't get anything. EDIT: I cleaned the js code like this: function sendRequest(mode) { alert("function called"); $('shoutbox').innerHTML= "Processing..."; if(mode == "shout") new Ajax.Request("process.php?mode=shout", { method: 'post', postBody: 'text='+ $F('shoutsub'), onComplete: showResponse }); } function showResponse(req) { $('shoutbox').innerHTML = req.responseText; } Now, I actually get the alert to come up, but the rest of the code doesn't seem to go through. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 i got it with both the WebDeveloper toolbar and FireBug for Firefox...google will get you those if you use those toolbars you'll discover that prototype.js, while you have the code to include it, the file doesn't exist on your server. http://divnx.net/javascript/prototype.js (404) Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 25, 2008 Author Share Posted June 25, 2008 Ok, that was weird. I'm pretty sure I uploaded it, I added it again, and now I've made progress. It says processing, then goes back to saying "Shoutbox resets everyday etc...". If you refresh the page, you will see the time, username, but no message. I'm guessing the problem is here in process.php: <?php include("includes/Connect.php"); include("includes/functions.php"); session_start(); if($_GET['mode'] == "shout") { if(isset($_POST['text'])) { $shomes = mysql_real_escape_string(addslashes($_POST['shoutext'])); $sshuser = mysql_real_escape_string(addslashes($_SESSION['username'])); $cshuser = mysql_real_escape_string(addslashes($_COOKIE['user'])); $shtime = mysql_real_escape_string(addslashes(date('h:i a'))); $shdate = mysql_real_escape_string(addslashes(date('m-j-Y'))); if($_SESSION['username']) { mysql_query("INSERT INTO shoutbox(user, time, date, message) VALUES('$sshuser', '$shtime', '$shdate', '$shomes')"); mysql_query("UPDATE users SET currency = currency + 1 WHERE username = '$sshuser'"); } else if($_COOKIE['user']) { mysql_query("INSERT INTO shoutbox(user, time, date, message) VALUES('$cshuser', '$shtime', '$shdate', '$shomes')"); mysql_query("UPDATE users SET currency = currency + 1 WHERE username = '$cshuser'"); } else { header("location: ajax.php?display=shout&err=1"); } header("location: ajax.php?display=shout"); } else { header("location: ajax.php?display=shout&err=2"); } } else { header("location: ajax.php?display=shout&err=3"); } ?> "$shomes = mysql_real_escape_string(addslashes($_POST['shoutext']));" It is not recognizing $_POST['shouttext'] from a different file. I know this is a php problem now, so I'll post over there if you want me to. Thank You So Much! Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 25, 2008 Author Share Posted June 25, 2008 Hah, now it won't even insert. I'll post this in PHP forums now. Thank You so much your help and time! :) :) Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 25, 2008 Share Posted June 25, 2008 Just logged in, and it looks like it's working well now, glad I could help. ...one last tip....on your login script, after you validate the credentials, instead of just showing the page, use a header() redirect back to itself. that way after they log in, if they hit refresh, it won't ask them if they want to re-post the data: <?php if(/*validate login info*/){ header('Location: '.$_SERVER['PHP_SELF']); exit; } //Failed login message here ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.