Page 1 of 5 123 ... LastLast
Results 1 to 10 of 48

Thread: More than one input to a form question - how to process?

  1. #1
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Cool More than one input to a form question - how to process?

    PHP Code:
    <?php

    if(isset($_POST['submit'])){

    $names = array($_POST['names']);

    foreach(
    $names as $value)
    {
    echo 
    'Hi '.$value.' <br />';
    }
    }
    else
    {
    echo 
    '<form method = "POST" action = "">';
    echo 
    '<p>Names</p>';
    echo 
    '<p><textarea rows="10" name="names" cols="37"></textarea></p>';
    echo 
    '<p><input type="submit" value="Submit" name="submit"></p>';
    echo 
    '</form>';
    }
    ?>
    the user is asked to enter some names (just for this example) and they can enter as many as they like.

    Example....
    Input - Paul Gina Emma John

    Output....
    Hi Paul Gina Emma John
    But...

    What I want is the following...
    Hi Paul
    Hi Gina
    Hi Emma
    Hi John
    I have a perfectly good reason for wanting to know how to do this. I don't often post real scripts anymore as I found myself just relying on others to fix the script and then I wouldn't learn anything. So I think up an example to use which needs fixing and then I learn from that!!

    Hoping someone can spot the mistake here and can help me to fix this.

    Thanks in advance.

    Smithster.

  2. #2
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Try
    Code:
    if(isset($_POST['submit'])){ 
    
    $names = preg_split("/ /",$_POST['names']); 
    
    foreach($names as $value) 
    { 
    echo 'Hi '.$value.' <br />'; 
    } 
    }
    or I thing explode() would work too.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  3. #3
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thanks Jas, unfortunately it just gives the same output. Hi once, with all names on the same line.

  4. #4
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Are you sure? It just worked for me. Your code gave the error you described, but mine output
    Hi dave
    Hi joe
    Hi john
    Try pasting this:
    Code:
    <?php 
    
    if(isset($_POST['submit'])){ 
    
    $names = preg_split("/ /",$_POST['names']); 
    
    foreach($names as $value) 
    { 
    echo 'Hi '.$value.' <br />'; 
    } 
    } 
    else 
    { 
    echo '<form method = "POST" action = "">'; 
    echo '<p>Names</p>'; 
    echo '<p><textarea rows="10" name="names" cols="37"></textarea></p>'; 
    echo '<p><input type="submit" value="Submit" name="submit"></p>'; 
    echo '</form>'; 
    } 
    ?>
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  5. #5
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    lol that is very strange! It is still doing exactly the same thing for me!! I even tried changing the file name to see if it was a refresh problem. Still outputs it in the same way for me!! All on one line!!

    Tried it again on another webserver, same output also. :|

  6. #6
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Post your code, maybe there is a typo. . . unlikely if you pasted it, but might as well check. This is very stange. What version of PHP? It is PHP 5, right?
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  7. #7
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    well, it's PHP5.22 on my local web server, but where the real thing is to be hosted, that is 4.44.

    PHP Code:
    <?php

    if(isset($_POST['submit'])){

    $names preg_split("/ /",$_POST['names']);

    foreach(
    $names as $value)

    {
    echo 
    'Hi '.$value.' <br />';
    }
    }
    else
    {
    echo 
    '<form method = "POST" action = "">';
    echo 
    '<p>Names</p>';
    echo 
    '<p><textarea rows="10" name="names" cols="37"></textarea></p>';
    echo 
    '<p><input type="submit" value="Submit" name="submit"></p>';
    echo 
    '</form>';
    }
    ?>
    Coppied and pasted so yes unlikely!! But it does happen!!

  8. #8
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Are you running this script on PHP5 or PHP4?
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

  9. #9
    Join Date
    Oct 2006
    Posts
    94
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    All testing is done on local webserver which runs PHP 5.22

    Having said that, any script I have ever written on local server, then uploaded to online server, even with different versions of PHP, I have never had compatibility issues.

  10. #10
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    Okay, it's not the PHP parser. Maybe the browser? Try another one and check the generated source code.

    EDIT: or try this code:
    Code:
    <?php 
    
    if(isset($_POST['submit'])){ 
    
    $names = preg_split("/ /",$_POST['names']); 
    
    for($i=0; $i<count($names); $i++) 
    { 
    echo 'Hi '.$names[$i].' <br>'; 
    } 
    } 
    else 
    { 
    echo '<form method = "POST" action = "">'; 
    echo '<p>Names</p>'; 
    echo '<p><textarea rows="10" name="names" cols="37"></textarea></p>'; 
    echo '<p><input type="submit" value="Submit" name="submit"></p>'; 
    echo '</form>'; 
    } 
    ?>
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

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
  •