Asked  8 Months ago    Answers:  5   Viewed   33 times

I want to check email availability but it's not working. and also I am new to javascript and ajax please help me.

his is my code email input with span to show output(for now there is no output)

<input class="input--style-4" id="email" type="email" name="email" required>
<span id="user-availability-status"></span>

JS

<script>
  $(document).ready(function() {
    $('#email').blur(function() {
      var email = $(this).val();

      $.ajax({
        url: 'includesemailAvailability.php',
        method: "POST",
        data: {
          email_val: email
        },
        success: function(data) {
          if (data != 0) {
            $('#user-availability-status').html('<span>Username blah not available</span>');
            $('#register').attr("disabled", true);
          } else {
            $('#user-availability-status').html('<span>Username blah Available</span>');
            $('#register').attr("disabled", false);
          }
        }
      })
    });
  });
</script>

PHP file


<?php

if (isset($_POST["email_val"])) {
    include("DbConn.php");
    $email = mysqli_real_escape_string($conn, $_POST["email_val"]);
    $query = "SELECT * FROM customer WHERE email = '" . $email . "'";
    $result = mysqli_query($conn, $query);
    echo mysqli_num_rows($result);
}

 Answers

29

You should check link I refered in comment its your complete answer.

here is a Simple example with your code.

include("DbConn.php");
    // Set alerts as array 
$error     = "";

    // I should just trrim and let you check if email is empty .lol 
if (empty($_POST["email_val"])) {
    $error .= "<p class='error'>Fill email value.</p>";

    //Check if this is a real email 
} elseif(!filter_var($_POST["email_val"],FILTER_VALIDATE_EMAIL)){
    $error .= "<p class='error'>Wrong email type.</p>";
}else{
    $email = mysqli_real_escape_string($conn, $_POST["email_val"]);

    //You should use prepare statement $email, Shame on you .lol    
    $query = "SELECT * FROM customer WHERE email = '{$email}'");
    $result = mysqli_query($conn, $query);
    echo mysqli_num_rows($result);
    $error .= "ok";
}
$data = array(
 'error'  => $error
);

This Jquery :

 $(document).ready(function(){
    $('#myform').submit(function(event){
    event.preventDefault();
    var formValues = $(this).serialize();
        $.ajax({
        url:"includesemailAvailability.php",
        method:"POST",
        data:formValues,
        dataType:"JSON",
            success:function(data){
                if(data.error === 'ok'){
                    $('#result').html(data.error);
                } else {
                    $('#result').html(data.error);
                    $('#myform')[0].reset();
                }
            }
        });
    });
});

And Html :

<form id="myform">
  <input class="input--style-4" id="email" type="email" name="email_val">
  <span id="result"></span>
  <button type="button" class="btn btn-primary">Send</button>
</form>
Wednesday, March 31, 2021
 
Besnik
answered 8 Months ago
93

"but it doesn't seem to work on my free 000webhost account"

A: They do not offer SMTP for free hosting. Use PHP's mail() function instead.

Notice the red x next to SMTP Server on their site => http://www.000webhost.com/ ?

enter image description here

Either use mail() or pay for it in order to use SMTP.

You can do it on their website, or upgrade from within the control panel.

Edit (Sept. 16, 2016): This answer was posted before they revamped their website. The URL where you can see the available options for free/paid services, can be see here https://www.000webhost.com/premium-web-hosting

This also goes for remote connections to a database.

This answer was edited in regards to a question I saw today which was related to my edit.

  • https://stackoverflow.com/q/39541929/1415724
Wednesday, March 31, 2021
 
OMGKurtNilsen
answered 8 Months ago
13

Post your javascript code.


A blind guess is : you are trying to setup your ajaxForm before it is actually present in the page.

If you want to run ajaxForm on '#brands_form_update', you have to request for it, insert it in the document, then call ajaxForm. This should give something like :

// $.get(...) sends the request
// $(..).append inserts the form inside the document
// $('#brands_form_update').ajaxForm() sets up the form
$.get('myDynamicForm.php', { ...data...}, function(html){
    $('*mySelector*').append(html);
    $('#brands_form_update').ajaxForm();
});
Wednesday, March 31, 2021
 
macha
answered 8 Months ago
33

Beware of using Gmail from different devices. google doesn't allow and block immediately a location from where an account is used where it isn't supposed to use (in the eyes of google ofcourse).

Wednesday, March 31, 2021
 
ajaybc
answered 8 Months ago
35

The first script it's not send through ajax:

 // run long running (later Download) PHP script
  console.log("Calling: PHP/fileDownload.php");
  window.location.href = 'PHP/fileDownload.php';

You simply redirect the user to another page, and because you have download headers in php, the file is downloaded in the same page.

You can easily achieve your scope through an iframe. You set the source of that iframe : 'PHP/fileDownload.php' and then simply call your ajax download checker.

Short example:

<iframe src="PHP/fileDownload.php">

<script>
        window.setTimeout(function(){startDownloadMonitoring()}, 1000);
        window.setTimeout(function(){startDownloadMonitoring()}, 3000);
        window.setTimeout(function(){startDownloadMonitoring()}, 5000);
        window.setTimeout(function(){startDownloadMonitoring()}, 7000);
        window.setTimeout(function(){startDownloadMonitoring()}, 9000);
        // .... blah blah
</script>
Saturday, May 29, 2021
 
TheFrack
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :