Probably, an AJAX request is what you need.
Assuming you`re using JQuery:
var mail = prompt("Exciting things to come, Sign up for our email list!!!");
$.post( "url_to_your_php_file", { "market":mail }, function( data ) {
if(data == true)
alert('Success!');
else
alert('Ooops! Try again later.');
});
You should update your PHP file to have the following code:
function add_Email_to_MarketingList($mail)
{
global $wpdb;
if($wpdb->insert('wp_marketing_emails', array('email' => $mail)))
return true;
return false;
}
add_Email_to_MarketingList($_POST['market']);
exit();
Also, you should use $_SERVER['HTTP_X_REQUESTED_WITH']
in PHP to detect any AJAX request. Your code would only work with AJAX requests and any direct access to the file would be denied.
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
/* AJAX Here */
}
else {
die('No direct access allowed.');
}
I would also use Validation FILTERS and filter_var
to check the e-mail. Never trust outside input.
POST
orGET
request to the PHP script, and it calls the function based on the parameters.