0

I have a form just to submit an email into a custom table of wp_wpa_watchlist.

The Form html is:

<form name="anyreg" id="any_reg" method="post">
                    <table>
                    <tr>
<td><input type="text" name="email" id="mail" placeholder="Enter Your Email"></td>
            <input type="hidden" name="id" value="<?php echo $auction ?>">
            <td><input type="submit" name="submitmail" id="submitmail" value="Submit"></td>
            </tr>

validation code + ajax call code is:

<script type="text/javascript">
$('#submitmail').click(function(event){

            event.preventDefault();

            var tre = true;


 var anymail = document.getElementById("mail").value;
            var atpos = anymail.indexOf("@");
            var dotpos = anymail.lastIndexOf(".");

                if (anymail==null || anymail=="")
                    {  
                    alert("Email cannot be Empty");
                    tre =false;  
                    }

                else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=anymail.length) 
                    {
                    alert("Please Enter A Valid Email Address");
                    tre =false;
                     }
            if(tre == true){
        $.ajax({url: '<?php echo $ajaxUrl; ?>',method:'POST',
                data:$('#any_reg').serialize()
                ,success: function(result){
                    if (result== 'INSERTED'){
                        alert("SUCCESS");   
                    }else
                    {
                        alert("Failed");
                    }

                },

        });
}
});
</script>

And My the ajax call file contains the following query:

<?php 
$parse_uri = explode( 'wp-content', $_SERVER['SCRIPT_FILENAME'] );
require_once( $parse_uri[0] . 'wp-load.php' );

global $wpdb;

$email=$_POST['email'];
$auction=intval($_GET['ID']);

if(!email_exists( $email ))
{
        $insert = "INSERT INTO wp_wpa_watchlist (auction_id,watch_email) VALUES('".$auction."', '".$email."')";
        $dbin = $wpdb -> query($insert);    

        if($dbin){
            echo "INSERTED";
        }
        else{
            echo "ERROR";
        }

}

else if 
    { 
        echo "Email Already Exists";
    }

?>

am very much newbie when it comes to ajax and ajax call. My problem is ajax call isnt happening. When i checked the console its showing Internal Error 500. Anyone please guide me

Jackson
  • 254
  • 1
  • 5
  • 20
  • 1
    Error 500 indicates that an error lies in your PHP code. Check your server's `error.log` for further details – Julien Lachal Jun 23 '17 at 13:09
  • Questions related to Ajax calls have been asked a lot of times in SO. Can you check this thread and see if this solves your issue https://stackoverflow.com/questions/21539305/use-ajax-to-send-html5-textarea-directly-without-html-form/21539883#21539883? – kabirbaidhya Jun 23 '17 at 13:16

0 Answers0