1

Please bear with me; trying my best to learn more Ajax. I am trying to Validate whether the Name of Event field in my form already exists in my table, but only if both were created by the same User. For example, if User 1 already has an event called Event1, the validation would check if there was a duplicate event name ONLY under User1.

I have the following snippet in a PHP/HTML form:

<div>Event Name: </div>
<input type="text" name="eventname" id="eventname" onblur="checkeventname()" onkeyup="restrict('eventname')" size="50" maxlength="75" />
<span id="eventnamestatus"></span>

This is my checkeventname function:

function checkeventname(){
  var nameofevent = _("eventname").value;
  if(nameofevent != ""){
    _("eventnamestatus").innerHTML = 'checking ...';
    var ajax = ajaxObj("POST", "eventcreationpage.php");
    ajax.onreadystatechange = function() {
      if(ajaxReturn(ajax) == true) {
        _("eventnamestatus").innerHTML = ajax.responseText;
      }
    }
    ajax.send("usernamecheck="+nameofevent);
  }
}

And here is the Ajax I put at the top of the page, which I am having trouble with:

<?php
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["eventnamecheck"])){
  include_once("php_includes/db_conx.php");
  $eventname = preg_replace('#[^a-z0-9]#i', '', $_POST['eventname']);
  $sql = "SELECT id FROM users WHERE eventname='$eventname' && eventcreator='$eventcreator' LIMIT 1";
  $query = mysqli_query($db_conx, $sql); 
  $eventname_check = mysqli_num_rows($query);

  if ($eventname_check < 1) {
    echo '<strong style="color:#009900;">' . $eventname . ' is not a duplicate name</strong>';
    exit();
  } else {
    echo '<strong style="color:#F00;">' . $eventname . ' is an event name already under your name</strong>';
    exit();
  }
}
?>

The webpage itself has the user variable carried over (eventcreationpage.php$eventcreator=User1) I am trying to send over the $eventcreator variable, which would be the User in this case, but I'm not quite sure how to do so.

bmceldowney
  • 2,287
  • 11
  • 19
blueapplez
  • 53
  • 4
  • I don't know much about PHP but I would recommend that you simply send back the boolean result from `$eventname_check` and render the HTML client side. For your problem heres an example of sending multiple params http://stackoverflow.com/questions/9713058/sending-post-data-with-a-xmlhttprequest – BillPull Jun 03 '15 at 21:01

1 Answers1

2

Set

ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

to show that this request will send form data. Next, on your server, you may use $_POST["userid"] to get the userid if you specified it via

ajax.send("userid=" + userid);

To send both userid and eventid, you may use

ajax.send("userid=" + userid + "&eventid=" + eventid);

If you get the userid only from PHP, you could render it into script. That would look like this:

ajax.send("userid=<?php echo $eventcreator; ?>&eventid=" + eventid);

which injects the user's name into the string. Make sure it is properly escaped though, if you allow special characters for user names though.

tom
  • 682
  • 6
  • 11