SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why doesn't this work?

    I have set this up so you type in data into a text box with a colon delimiter (Ex... Title: Explanation) and it will split the string and generates the code that I can then paste into an HTML document and don't have to worry about typing in code to put the data into an HTML table.

    The only problem I have is that when you hit the button when there is no data in the text box, it gives an error. I am trying to use a if (isset()) statement to only run this part when something actually is in the box, but the script runs whether there's anything in the box or not.

    I am still new to PHP so any tips on how to clean up the programming or just any suggestions in general are appreciated as well.

    Here is the code

    THis goes between the head tags:

    <style type="text/css">
    textarea {
    display: block;
    width: 66&#37;;
    }
    </style>

    THis goes between the body tags:

    <form action="conphp2.php" method="post">
    <div> <label for="specs">Entries</label>
    <textarea id="specs" name="specs" rows="10" cols="40"></textarea>
    </div>
    <div> <input type="submit" value="GO"/>
    </div>
    </form>


    and here's the PHP code:


    <?php
    $specs = $_POST['specs'];
    if (isset($specs))
    {
    $speclist = explode("\n", $specs);
    echo htmlspecialchars('<table>');
    foreach($speclist as $specitems)
    {
    echo htmlspecialchars('<tr>');
    $pieces = explode(":", $specitems);
    echo htmlspecialchars ('<td>' . $pieces[0] . '</td>');
    echo htmlspecialchars ('<td>' . $pieces[1] . '</td>');
    echo htmlspecialchars('</tr>');
    }
    echo htmlspecialchars('</table');
    }
    ?>
    Last edited by michaelk46; Sep 10, 2009 at 17:54. Reason: clarification

  2. #2
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    $specs is always set, but it is empty.

    Try this

    PHP Code:
    <?php
    if(!empty($_POST['specs'])){
     
    $specs $_POST['specs'];
    or even easier

    PHP Code:
    <?php
    if($_POST['specs'] != ""){
     
    $specs $_POST['specs'];

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2006
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool...thanks man

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    There are many ways to validate user input. All isset() does is test if it's defined. You could also use trim() to remove beginning and ending whitespace, and then empty() to see if anything's there after you trim() it.


Bookmarks

Posting Permissions

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