Jump to content

ephemeral

New Members
  • Posts

    6
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ephemeral's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. I made a post here a few months ago pertaining to a strange Ajax issue I am experiencing: http://www.phpfreaks.com/forums/index.php/topic,281457.msg1333883.html#msg1333883 I have let this question slide because of holidays and projects of greater priority, but now I am able to work on this page again and still find myself stumped by this odd behavior.
  2. I'm having two Ajax-related problems on a page I am working on at the moment. You can see it here: http://www.utm.edu/departments/acadpro/library/openmic/add2db.php On this page, I have two buttons that use Ajax to fetch two separate forms and put them into a chosen div; that part works like a charm. On said forms, there is a div that is meant to display any necessary error messages when the submit button is clicked. However, on the first time the page is visited and the form is chosen, the div won't display the error message. Only after refreshing the page/choosing the form a second time will the message show. I've been checking the response from the server via Firebug and the response is correct, it just seems like the page isn't updating correctly. Secondly, once I refresh or choose the form again and the error message begins to show, if I enter values into the form fields and should be getting a different error message, it never updates. Once again, the server response is correct, but for some reason the page just isn't updating to reflect the new message. I have been using the date field on the New Event form for testing; if you have Firebug, you should be able to see that I am getting the desired response from the server, but the HTML isn't changing with it. I have tried both using my own Ajax functions and using jQuery's Ajax implementation, but both give me the same problems. What am I doing wrong? (PS - The code spans over two files -- http://www.utm.edu/departments/acadpro/library/openmic/add2db.php and http://www.utm.edu/departments/acadpro/library/openmic/js/add2db.js -- so unless someone wants to see it posted here, I won't include the large code blocks. The files are live at the urls, and are attached in a zipped folder. ) [attachment deleted by admin]
  3. I solved the problem...it would have helped if I had put a $ in front of $testVal when checking it in a later else if. *headdesk* Leaving that out changed the value of the response array. Thanks you guys.
  4. I have tried printing both values directly before the if statement, and they come out to be exactly the same...if that is what you mean by my "input isn't what I am expecting," lemmin. Also, there is a part of that program that is functioning a little later that wouldn't unless $testVal is set to be true...and the only time $testVal is true is when the else is executed. Mark, I tried testing for string equality and that didn't work, so I went through and tried strcmp() and levenshtein(). That was explained in my first post.
  5. Yes, I have tried printing something from inside and it is never printed. Also, there is a part of my code a little later than only evaluates if $testVal is set to true, which is done in the "else" of that particular if statement, and that part depending on $testVal is running.
  6. The following code is part of an AJAX-driven page that I am currently constructing: <?php if(@$_REQUEST['action'] == 'check_date' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { // check if requested via ajax echo json_encode(check_date($_REQUEST['popUpDate'])); exit; // only print json version of msg } else if (@$_REQUEST['action'] == 'register') { check_date($_REQUEST['popUpDate']); if (!$resp['ok']) { $message = "There was a problem with your request: \n"; $message .= $resp['msg']; } } function check_date($date) { $resp = array(); $events = get_events(); if (!$date) { $resp = array( 'ok' => false, 'msg' => "You must enter a date" ); } else if (!preg_match("%^((?:19|20)\d\d)/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])$%", $date)) { $resp = array( 'ok' => false, 'msg' => "Date must be in format YYYY/MM/DD" ); } else { // check if date is already taken $arraySize = count($events); $testVal = false; for ($myRow = 0; $myRow < $arraySize; $myRow++) { $arb = $events[$myRow]['Date']; if (levenshtein($arb, $date) == 0) { $resp = array( 'ok' => false, 'msg' => "There is already an event on this date" ); } else { $testVal = true; } } // end for //print $testVal; // check if date is actually valid $groups = array(); preg_match('%^((?:19|20)\d\d)/(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])$%', $date, $groups); if ($groups[3] == 31 && ($groups[2] == 4 || $groups[2] == 6 || $groups[2] == 9 || $groups[2] == 11)) { $resp = array( 'ok' => false, 'msg' => "Date is 31 in a month with only 30 days" ); } else if ($groups[2] == 2 and $groups[3] >= 30) { $resp = array( 'ok' => false, 'msg' => "Date is 30 or 31 in February" ); } else if ($groups[2] == 2 and $groups[3] == 29 and !($groups[1] % 4 == 0 and ($groups[1] % 100 != 0 or $groups[1] % 400 == 0))) { $resp = array( 'ok' => false, 'msg' => "Date is 29 in February and it is not a leap year" ); } else if (testVal == true) { $resp = array( 'ok' => true, 'msg' => "This date is available" ); } // end if } // end else return $resp; } // end function function get_events() { //Database connection details $host = "mysql.utm.edu"; $mysql_user = "library"; $mysql_password = "pmlib"; $mysql_db = "library"; //make connection with mysql and select the database $mysql_connect = mysql_connect($host, $mysql_user, $mysql_password); $db_select = mysql_select_db($mysql_db); //getting some times for later use (in query and performance) $nowTime = time(); $today = date("Y/n/j", time()); $results = mysql_query("SELECT DATE_FORMAT(eventTime, '%Y/%m/%d') as eDate,DATE_FORMAT(eventTime, '%H:%i') as eTime,name FROM eventcal_test WHERE eventTime >= '$today' ORDER BY eDate ASC") or die(mysql_error()); while($row = mysql_fetch_assoc($results)) { $events[] = array('Date' => $row['eDate'], 'Time' => $row['eTime'], 'Performer' => $row['name']); } //print_r($events); return $events; } // end function // tells which form to post - event form if ($_POST["formChoice"] == "event") { //if (!isset($_POST['popUpDate']) && !isset($_POST['startTime']) && !isset($_POST['endTime'])) { // if no values set, post form ?> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST" id="eventform"> <div class="tablecontainer"> <div class="cap"> <h2>Add a New Event</h2> </div> <div id="dateRow" class="tr"> <div id="dateLabel" class="td">Date: </div> <div id="dateInput" class="td"><input id="popUpDate" name="popUpDate" class="popUpDate" autocomplete="off" size="30" value="<?=@$_REQUEST['popUpDate']?>" /></div> <div id="dateError" class="td hidden"></div> </div> <div id="startTimeRow" class="tr"> <div id="startTimeLabel" class="td">Start Time: </div> <div id="startTimeInput" class="td"><input id="startTime" name="startTime" class="pickTime" autocomplete="off" size="30" /></div> <div id="startTimeError" class="td hidden"></div> </div> <div id="endTimeRow" class="tr"> <div id="endTimeLabel" class="td">End Time: </div> <div id="endTimeInput" class="td"><input id="endTime" name="endTime" class="pickTime" autocomplete="off" size="30" /></div> <div id="endTimeError" class="td hidden"></div> </div> </div> <input type="submit" value="Add Event" name="add" /> </form> <?php //} elseif (isset($_POST['popUpDate'] && isset($_POST['startTime']) && isset($_POST['endTime'])) { //} // tells which form to post - performance form } else if ($_POST["formChoice"] == "performance") { ?> <form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST"> <div class="tablecontainer"> <div class="cap"> <h2>Add a New Performance to an Existing Event</h2> </div> <div class="tr"> <div class="td">Date: </div> <div class="td"> <select size="1" name="date" id="date"> <option value="" selected="selected" /> </select> </div> </div> <div class="tr"> <div class="td">Time: </div> <div class="td"> <select size="1" name="time" id="time"> <option value="" selected="seleted" /> </select> </div> </div> <div class="tr"> <div class="td">Name of Performer: </div> <div class="td"> <input type="text" id="name" name="name" size="30" /> </div> </div> </div> <input type="submit" value="Add Performance" name="add" /> </form> <?php } ?> I am having a problem that seems strange. In this section of code: // check if date is already taken $arraySize = count($events); $testVal = false; for ($myRow = 0; $myRow < $arraySize; $myRow++) { $arb = $events[$myRow]['Date']; if (levenshtein($arb, $date) == 0) { $resp = array( 'ok' => false, 'msg' => "There is already an event on this date" ); } else { $testVal = true; } } // end for The if statement is supposed to check if the two parameters in the levenshtein() function are the same. (Note: What I want to do is check if the strings are the same. I've tried checking if they are equal with ==, using strcmp(), and levenshtein(). Directly before the if statement, inside the for loop, I tried printing levenshtein($arb, $date), and the result I got was zero -- they were equal -- but when placed inside the if like I have above, the statement does not evaluate to true, thus skipping to the else) I have used is_string and both $arb and $date are strings. $arraySize is 1, which is correct for the $events array. When printing $arb and $date, they are both exactly '2009/12/29'. I can't quite figure out why that condition doesn't evaluate to true and enter the if. Thanks ahead of time for any replies, and sorry if I have done anything wrong...this is my first post here.
×
×
  • 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.