Jump to content

[SOLVED] help with simple ajax - show message on form submission


scarhand

Recommended Posts

im trying to get this code to, on form submission, check in a mysql database if their IP exists and then show a message under the submit button.

 

if it exists, show a message under the submit button that says "you already reported this"

 

if it doesnt exist, show a message that says "you havent reported this"

 

heres what i have so far that doesnt work:

 

the page with the form:

<form method="post" onsubmit="sendReport(); return false;">
<input type="submit" value="Click here to report">
</form>
  
<div id="reportmsg"></div>

 

the javascript/ajax:

function ajaxFunction()
{
  var ajaxRequest;

  try
  {
    ajaxRequest = new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        // browser does not support
        alert("Browser does not support Shoutbox requests.");
        return false;
      }
    }

  }
  return ajaxRequest;
}

function handleReportResponse()
{
  if (htmlRequest.readyState==4 && htmlRequest.status==200)
  {
    document.getElementById("reportmsg").innerHTML = htmlRequest.responseText;
  }
}

function sendReport()
{
  htmlRequest = ajaxFunction();

  htmlRequest.open('POST', 'report.php');
  htmlRequest.onreadystatechange = handleReportResponse;
  htmlRequest.send(null);
}

 

the php script:

<?php

include "connect.php"; // connect to the database

$ip = $_SERVER["REMOTE_ADDR"];

$sql = mysql_query("SELECT * FROM reports WHERE ip='$ip'")
$sql_count = mysql_num_rows($sql);

if ($sql_count != 0)
  echo "You have already reported";
else
  echo "You have not reported";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.