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";

?>

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.