Web Analytics Made Easy -
StatCounter how do i do so it doesnt echo a blank input ? - CodingForum

Announcement

Collapse
No announcement yet.

how do i do so it doesnt echo a blank input ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • how do i do so it doesnt echo a blank input ?

    I have a form, and it a javascript calls a php that echo the input on an "ul" using ajax.

    the thing is that, it echos everything, even when the form is blank, with no text, it echos that "blank" value.

    How do i do so it doesnt echo when the input is blank?


    Code:
    <?php
    if(isset($_POST['addcontentbox'])){
    /* Connection to Database */
    include('config.php');
    /* Prevent Query Injection */
    $message = mysql_real_escape_string($_POST['addcontentbox']);
    /*echo*/
    $sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
    mysql_query($sql);
    echo $message;
    } else { echo '0'; }
    ?>
    Note:
    addcontentbox = the input id/name.
    config.php is the databse config
    "wall" is the id of the unsorted list.


    Any sugestions? guide? =[

  • #2
    Originally posted by Nightcrawler89 View Post
    I have a form, and it a javascript calls a php that echo the input on an "ul" using ajax.

    the thing is that, it echos everything, even when the form is blank, with no text, it echos that "blank" value.

    How do i do so it doesnt echo when the input is blank?


    Code:
    <?php
    if(isset($_POST['addcontentbox'])){
    /* Connection to Database */
    include('config.php');
    /* Prevent Query Injection */
    $message = mysql_real_escape_string($_POST['addcontentbox']);
    /*echo*/
    $sql = 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
    mysql_query($sql);
    echo $message;
    } else { echo '0'; }
    ?>
    Note:
    addcontentbox = the input id/name.
    config.php is the databse config
    "wall" is the id of the unsorted list.


    Any sugestions? guide? =[
    Wrap your code in PHP tags, not CODE tags

    But, to your main question, you need to check if $_POST['addcontentbox'] is empty. Do this: (I formatted your code better as well, to make it more readable )
    PHP Code:
    <?php
    if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) ){

        
    // Connection to Database
        
    include('config.php');

        
    // Prevent Query Injection
        
    $message mysql_real_escape_string($_POST['addcontentbox']);
        
        
    // echo
        
    $sql 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
        
    mysql_query($sql);

        echo 
    $message;
    }
    else { 
        echo 
    'You must fill out the content box!';
    }
    ?>
    Should work
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

    Comment


    • #3
      Originally posted by BluePanther View Post
      Wrap your code in PHP tags, not CODE tags

      But, to your main question, you need to check if $_POST['addcontentbox'] is empty. Do this: (I formatted your code better as well, to make it more readable )
      PHP Code:
      <?php
      if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) ){

          
      // Connection to Database
          
      include('config.php');

          
      // Prevent Query Injection
          
      $message mysql_real_escape_string($_POST['addcontentbox']);
          
          
      // echo
          
      $sql 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
          
      mysql_query($sql);

          echo 
      $message;
      }
      else { 
          echo 
      'You must fill out the content box!';
      }
      ?>
      Should work
      Thanks !!! , looks much more readable now, but, its not working =( ...

      maybe i have to do it thru javascript.

      Comment


      • #4
        Originally posted by Nightcrawler89 View Post
        Thanks !!! , looks much more readable now, but, its not working =( ...

        maybe i have to do it thru javascript.
        Any more information than 'it's not working'?

        Only touch javascript if you want animation. What exactly is the result you're looking for?
        Useful function to retrieve difference in times
        The best PHP resource
        A good PHP FAQ
        PLEASE remember to wrap your code in [PHP] tags.
        PHP Code:
        // Replace this
        if(isset($_POST['submitButton']))
        // With this
        if(!empty($_POST))
        // Then check for values/forms. Some IE versions don't send the submit button 
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

        Comment


        • #5
          Originally posted by BluePanther View Post
          (I formatted your code better as well, to make it more readable )
          No, no, no... thats not readable

          This is much better
          PHP Code:
          <?php
          if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) )
             {
             
          // Connection to Database
             
          include('config.php');

             
          // Prevent Query Injection
             
          $message mysql_real_escape_string($_POST['addcontentbox']);

             
          // echo
             
          $sql 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
             
          mysql_query($sql);

             echo 
          $message;
             }
          else
             { 
             echo 
          'You must fill out the content box!';
             }
          ?>

          Much easier to see the opening and closing brace relations Plus it works better in Notepad++

          (I never understood the { at the end of one line and the } at the beginning of another - along with other closing braces - far too confusing!)
          "Tango says double quotes with a single ( ' ) quote in the middle"
          '$Name says single quotes with a double ( " ) quote in the middle'
          "Tango says double quotes ( \" ) must escape a double quote"
          '$Name single quotes ( \' ) must escape a single quote'

          Comment


          • #6
            Originally posted by tangoforce View Post
            No, no, no... thats not readable

            This is much better
            PHP Code:
            <?php
            if( isset($_POST['addcontentbox']) && !empty($_POST['addcontentbox']) )
               {
               
            // Connection to Database
               
            include('config.php');

               
            // Prevent Query Injection
               
            $message mysql_real_escape_string($_POST['addcontentbox']);

               
            // echo
               
            $sql 'INSERT INTO WALL (message) VALUES( "'.$message.'")';
               
            mysql_query($sql);

               echo 
            $message;
               }
            else
               { 
               echo 
            'You must fill out the content box!';
               }
            ?>

            Much easier to see the opening and closing brace relations Plus it works better in Notepad++

            (I never understood the { at the end of one line and the } at the beginning of another - along with other closing braces - far too confusing!)
            PREFERENCE :P.

            I don't use notepad++ anyway. I'm a netbeans kinda guy.

            I lay it out like that because of my python background:

            Code:
            if(whatever):
               do something
            Incase you're not familiar with python syntax
            Useful function to retrieve difference in times
            The best PHP resource
            A good PHP FAQ
            PLEASE remember to wrap your code in [PHP] tags.
            PHP Code:
            // Replace this
            if(isset($_POST['submitButton']))
            // With this
            if(!empty($_POST))
            // Then check for values/forms. Some IE versions don't send the submit button 
            Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

            Comment


            • #7
              Originally posted by BluePanther View Post
              Any more information than 'it's not working'?

              Only touch javascript if you want animation. What exactly is the result you're looking for?
              lol, yes sorry. I mean, its still the same, everytime the text input is submited it echos, but is doign echo when is blank also, so it is posting blank spaces when the input is submited without any text on it. =\.


              Idk if this would help, but this is the javascript that calls the submit action:

              Code:
              <script type="text/javascript">
              $(document).ready(function(){
              $("form#postbar_add_post").submit(function() {
              
              var addcontentbox = $('#addcontentbox').attr('value');
              
              $.ajax({
              type: "POST",
              url: "postear.php",
              data:"addcontentbox="+ addcontentbox,
              success: function(){
              $("ul#wall").prepend("<li>"+addcontentbox+"</li>");
              $("ul#wall li:first").fadeIn();
              document.postbar_add_post.addcontentbox.value='';
              }
              });
              return false;
              });
              });
              </script>
              postbar_add_post = form name.
              addcontentbox = text input id.
              ul#wall = the ul where the text is posted to.

              Comment

              Working...
              X