Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/28/2017 in all areas

  1. 1 point
  2. Why use success and error ajax functions when you can use .done and .fail? .done and then you do all the checks in the php script even the return so that way you don't need to do that much of a javascript code in order to display an message. For example my way i use to display messages with jquery is very simple. For example index.php <div id="return_php" style="display: none;"></div> <form id="test"> <input type="text" name="something" placeholder="Insert something ..."> <input type="submit"> </form> <script> $('#test').submit(function(e){ e.preventDefault(); $.ajax({ url: 'path/to/something.php', data: $(this).serialize(), dataType: 'html', type: 'POST' }) .done(function(data){ $('#return_php').fadeIn(400).html(data).delay(5000).fadeOut(400); }) .fail(function(){ $('#return_php').fadeIn(400).html('Something went wrong with the ajax script !').delay(5000).fadeOut(400); }) }); </script> This way you just call the php page in which you do the checks and whatever stuff and then you echo an alert which will then be displayed into the return_php div. The .delay(5000) acts as a delay for the fadeOut function, after 5 seconds the message will fade away. something.php $something = $_POST['something']; if(empty($something)) { echo "Something needs to be filled in !"; //This message will be then returned into the div return_php on the index.php page } else { //Example of database try { $sql = "SELECT something FROM something WHERE something = :something"; $stmt = $db->prepare($sql); //$db is from a config file you have to include $stmt->bindParam(":something", $something); $stmt->execute(); $rowCount = $stmt->rowCount(); //This works for every case, at least i use it this way and for me works just fine. if($rowCount > 0) //let us presume that the something has more rows { echo "Ok, there is something in that something !"; } else { echo "There is nothing in something with the something you typed !"; } } catch (PDOException $e) { echo "Error: " . $e->getMesage(); //This will be showed into the return div aswell as the echo is still a part of this page, and this page is called by ajax. } } Now, this is my way to do it time to time when i need live php script calling on my page without needing my page to refresh. Hope it helped at least for your issue with displaying messages. EDIT I've read again through your post and i think i figured out what is all about. When i try to use that ereg_replace() php function is always throwing me an Uncaught error as the function doesn't exist. This may be influencing how you are selecting the data by that variable. Instead of ereg_replace use preg_replace, It does the same thing. $email = $_POST['email']; $email = preg_replace('/\s+/, '', $email); NOTE! Some of the code i wrote may or may not fit your code if you copy paste it as it doesn't contain the same variables or functions that you wrote. You may need to adapt it to your use.
    1 point
×
×
  • Create New...