Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:36:41+00:00 2026-06-16T20:36:41+00:00

I’m not really certain where the problem lies but I am passing a variable

  • 0

I’m not really certain where the problem lies but I am passing a variable through ajax and it is not being caught if I pass a blank when I check it with !$varname

Here is my ajax function:

var subscribe = function(){
  var dataString = $("#subinput").val();
   $.ajax ({
   url: '<?php echo $path ?>',
   type: 'POST',
   data: 'email=' + dataString.value,
   datatype: 'JSON',
   success: function(results){
     if(results.err == '1'){
        $('.onconfirmation').css('color','#f00');
     }else{
        $('.onconfirmation').css('color','#5A5A5A');
     }

     $('.onconfirmation').innerHTML(results.message);
     $('.onconfirmation').fadeIn();
     //alert(results);
   }
 });

and here is my PHP:

<?php
$email = $_POST['email'];

if(!$email){
    $o['err'] = '1';
    $o['message'] = 'Please do not leave this field blank';
}/*elseif(filter_var($email, FILTER_VALIDATE_EMAIL)){
    $o['err'] = '1';
    $o['message'] = 'Please enter a valid email address';
}else{
    $o['err'] = '0';
    $o['message'] = 'Thank you for your subscription';
}*/

ob_start();
var_dump($o);
$out = ob_get_clean();

mail('[REDACTED]','debug',$out);
//$o = json_encode($o);
//return ($o);
?>

As you can see it’s in a debugging state at the moment, but if I pass a blank value through into this, the email I am getting is NULL. If I email myself the $email variable instead of the $out variable, the email I get is undefined, but if I remove the ! from the if statement, the email I get is:

array(2) {
  ["err"]=>
  string(1) "1"
  ["message"]=>
  string(36) "Please do not leave this field blank"
} 

I’m sure I am just missing something awfully simple, I always am, but I honestly can’t figure this one out. Any help would be massively appreciated. Cheers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:36:42+00:00Added an answer on June 16, 2026 at 8:36 pm
    var subscribe = function(){
      var dataString = $("#subinput").val();
       $.ajax ({
       url: '<?php echo $path ?>',
       type: 'POST',
       data: { email : dataString },
       datatype: 'JSON',
       success: function(results){
         if(results.err == '1'){
            $('.onconfirmation').css('color','#f00');
         }else{
            $('.onconfirmation').css('color','#5A5A5A');
         }
    
         $('.onconfirmation').innerHTML(results.message);
         $('.onconfirmation').fadeIn();
         //alert(results);
       }
     });
    

    see the line

    data: { email : dataString }

    Once you have the val() you shouldn’t use ” .value ” and when POST-ing data this way is the correct way to add keys and values.

    At PHP do those things..

    <?php
    $o = array();
    
    if(!isset($_POST['email']) OR empty($_POST['email'])){
        $o['err'] = '1';
        $o['message'] = 'Please do not leave this field blank';
        echo json_encode($o);
        exit();
    }
    
    $email = $_POST['email']; 
    
    .......other code
    

    The exit() stops PHP from reading the next lines at your file so IF emial is not set or empty it wont do any further tasks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I need a function that will clean a strings' special characters. I do NOT
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.