SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Keep field filled betwwen 2 functions!?

    I can't figure this out. I'm building a form with error check, and if there is an error it should return to form but with the allready correct fields still filled in. Normally I don't have any problems with this, but this form is a little different...

    The form function:
    PHP Code:
    function addmembers(){
        
        global 
    $prefix,$db
        global 
    $err_fname0,$err_fname1
        global 
    $err_lname0,$err_lname1;
        global 
    $err_email0,$err_email1;
        global 
    $fname0,$fname1,$lname0,$lname1,$email0,$email1;

    echo 
    '<form method="POST" action="users.php?site=addmembers">';
    echo 
    '<table><tr>
            <td>
                '
    ._MALE.' <input type="radio" name="sex0" value="male" checked />
                &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex0" value="female" />
            </td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._FIRST_NAME.':</td>
            <td><input name="fname[]" type="text" size="42" value="'
    .$fname0.'"> '.$err_fname0.'</td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._LAST_NAMES.':</td>
            <td><input name="lname[]" type="text" size="42" value="'
    .$lname0.'"> '.$err_lname0.'</td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._EMAIL.':</td>
            <td><input name="email[]" type="text" size="42" value="'
    .$email0.'"> '.$err_email0.'</td>
        </tr>
        <tr>
            <td colspan="2"><br><div style="border-bottom:1px solid #669900;"></div><br></td>
        </tr>
        <tr>
            <td></td>
            <td>
                '
    ._MALE.' <input type="radio" name="sex1" value="male" checked />
                &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex1" value="female" />
            </td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._FIRST_NAME.':</td>
            <td><input name="fname[]" type="text" size="42" value="'
    .$fname1.'"> '.$err_fname1.'</td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._LAST_NAMES.':</td>
            <td><input name="lname[]" type="text" size="42" value="'
    .$lname1.'"> '.$err_lname1.'</td>
        </tr>
        <tr>
            <td align="right"><div align="right">'
    ._EMAIL.':</td>
            <td><input name="email[]" type="text" size="42" value="'
    .$email1.'"> '.$err_email1.'</td>
        </tr>
        <tr>
            <td colspan="2"></td>
        </tr>
    </table>'

    And the formcheck:
    PHP Code:
    function do_addmembers(){
        
        global 
    $prefix,$db
        global 
    $err_fname0,$err_fname1
        global 
    $err_lname0,$err_lname1;
        global 
    $err_email0,$err_email1;
        global 
    $fname0,$fname1,$lname0,$lname1,$email0,$email1;
        
        
    $firstname $_POST['fname']; 
        
    $lastname $_POST['lname'];
        
    $email $_POST['email'];
        
    $sex0 $_POST['sex0'];
        
    $sex1 $_POST['sex1'];
        
        for (
    $i 0$i count($firstname); $i++) {
            
            
    $nemail $email[$i];
            
    $nfname $firstname[$i];
            
    $nlname $lastname[$i];
            
    $nsex $_POST['sex'.$i.''];
            
            if((
    $nfname || $nlname) && !$nemail){
                        
                
    $reqmsg'<div style="padding:3px;color:#FFF;background-color:#F00;width:100%;text-align:center;">'._ONE_OR_MORE_EMAIL_INCORRECT.'</div>';
                
    $err_email $reqmsg;
                
    addmembers();
                exit();
                
            }
            
            
    /// INSERT INTO DB /// 
    Hope somebody can help...

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're trying to use variables which don't exist. For example, nowhere in the posted code do you define $fname0, and this mistake is made multiple times. You do have a variable named $nfname though.

    Consider passing your variables as arguments to your functions. Looking at your code, the current way you have excessively used the global keyword implies some really bad design unless there's other parts of the program that need these values aside from these functions.


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
  •