Jump to content

[SOLVED] Ajax Function not being called


Nexy

Recommended Posts

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! :)

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  :)

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.