Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 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
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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

  • #3
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote 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.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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?

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    Quote 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!)
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote 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

  • #7
    New Coder
    Join Date
    Aug 2011
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote 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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •