GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Sep-2006, 10:33
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road
Question

Variable scope question


Hi folks,
I guess I've caused enough trouble in the C forum, so...

Please. I'm even newer to this than I was with C, so some of my nomenclature may not be exactly correct...

I have inherited a pre-existing web-site with no database access, all HTML and JS. The designer has added forms at the customer's request to create a member/email list, and I'm having problems passing results of my PHP scripts back to the HTML page (if it's even possible).

I'm trying to do this without having all of the HTML code being radically modified.

example:

Sone of the HTML code (straightforward), but I know will need to modified to deal with the results of the PHP script.

HTML Code:
<form method="post" action="..\cgi\registrationNew.php" enctype="multipart/form-data" name="registrationSubmit" id="registrationSubmit"> <fieldset> <legend><span class="style4">Resistration Submission</span></legend> <fieldset> <legend><span class="style6">Personal Information</span></legend> <table width="350" border="0" cellspacing="0" cellpadding="0" summary="Please enter the following information."> <tr> <td width="146"><div align="left" class="style21" First Name </div></td> <td colspan="2"><div align="left"> <input name="firstNameField" type="text" id="firstNameField" tabindex="1" size="20" maxlength="50" /> </div></td> </tr> <tr> <td><div align="left"><span class="style21">Last Name </span></div></td> <td colspan="2"><div align="left"> <input name="lastNameField" type="text" id="lastNameField" tabindex="2" size="20" maxlength="50" /> </div></td> . . .

some of the PHP script registrationNew.php - it's only a prototype to show what I'm after. I know it's rife with no-nos and stupid newbie tricks.

PHP Code:

<script language="php">

$result = "ok";
$type = "none";

// return to form page

function return_home()
{
   header( "Location: http://127.0.0.1        /registration/registrationSubmission.html" );
}

// cannot have blank name, city, or state

if( ( !$_POST[firstNameField] ) ||  ( !$_POST[lastNameField] ) || 
    ( !$_POST[cityField] ) || ( !$_POST[state] ) ) 
{
    // indicate that the above fields cannot be blank    
    $result = "error";
    $type = "name_city";

    return_home();  // yea, I know this a problem too
    exit;
}

// make sure the email address entered is the same in both places(and is not blank)
if ( ( $_POST[emailField]  ) && ( $_POST[confirmEmailField] ) )
{
   if ( strcmp( $_POST[emailField], $_POST[confirmEmailField] ) != 0 ) 
   {
        result = "error";
        type = "password_match";    
             
        return_home();
        exit;
    } 
    else 
    {
        
        return_home();
        exit;
    }
}

/* if there are no errors, write data to new record in the database */
.
.
.
</script> 


What I'm after is to be able to, after returning to the HTML page is to display (in HTML) an error if one occurs in an area on the same page as the form and "force the user to re-enter the info in to the form.

I know this is PHP 099, but new to me and any help to get me on track wouod be greatly appreciated! I have read and read different tutorial sites plus two VERY large books I have and just can't divine what it is I'm looking for.

Thanks in advance,
Mike M.
  #2  
Old 21-Sep-2006, 11:12
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 749
admin will become famous soon enough

Re: Variable scope question


Basically you're asking if it's possible to display an error message on the original web page i.e. registrationSubmission.html?

The answer is no, at least not with PHP. It's not exactly impossible either but you may need to use some Javascript, at least.

You could do the redirect appending a query string to the original document and use that query string value to display an error message using Javascript. It's not what people do normally but you wanted to know if it was possible.

Also, a good habit to develop is this:

PHP Code:

<?php
// check if some required user input was submitted
if( isset($_POST['firstNameField'])  &&  $_POST['firstNameField'] )
{
  // the rest


I won't comment further on your PHP script because that's not what you asked for.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #3  
Old 22-Sep-2006, 10:04
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road

Re: Variable scope question


Thank you very much for your response!

Quote:
Originally Posted by admin
Basically The answer is no, at least not with PHP. It's not exactly impossible either but you may need to use some Javascript, at least.

Here's an example of some HTML from another site that appears to be doing what I want (without the newbie errors) -sorry for the formatting, it ws the best I could do...
HTML Code:
<form action="http://forums.dser.net/index.php" method="post" name="REG" onsubmit="return validate_reg_form(event)"> <input type="hidden" name="act" value="Reg" /> <input type="hidden" name="termsread" value="1" /> <input type="hidden" name="agree_to_terms" value="1" /> <input type="hidden" name="CODE" value="02" /> <input type="hidden" name="coppa_user" value="0" /> <div class="borderwrap"> <div class="maintitle"><img src='style_images/dic2.1.6b/nav_m.gif' border='0' alt='&gt;' width='8' height='8' />&nbsp;Registration Form</div> <div class="formsubtitle">Please ensure that you complete all the fields fully, taking particular care over the password fields.</div> <div class="tablepad"> <table class='ipbtable' cellspacing="0" width="100%"> <tr> <td width="100%"> <fieldset class="row3"> . . . <div class="desc">I agree to the terms of this registration and wish to proceed.</div><br /> <input type="submit" value="Submit my registration &gt; &gt;" /> </div> </td> </tr> </table> </div> </div> </form>
Apparently, there's some way to do what I'm after. There is no Java script in the header or elsewhere that has anything to do with the form. Of course, I do not know what's in index.php, but again that's where I need help.

Thanks for your coding advice. Why don't books often not use best practice when demonstrating coding examples, even in advanced chapters?

You stated:
I won't comment further on your PHP script because that's not what you asked for.

Thank you, but I would accept any advice at anytime, even if it's off-line.

So, can anyone use my last two posts and give me any advice. Surely what I'm asking for is not impossible (based on the HTML included in this post).

Thanks again, folks - I always appreciate the help I get here, that's why I hang around.

Cheers,
Mike M
  #4  
Old 22-Sep-2006, 15:02
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road

Re: Variable scope question


Quote:
Originally Posted by wmmccoy0910
Thank you very much for your response!



Here's an example of some HTML from another site that appears to be doing what I want (without the newbie errors) -sorry for the formatting, it ws the best I could do...
HTML Code:
Apparently, there's some way to do what I'm after. There is no Java script in the header or elsewhere that has anything to do with the form. Of course, I do not know what's in index.php, but again that's where I need help.

Thanks for your coding advice. Why don't books often not use best practice when demonstrating coding examples, even in advanced chapters?

You stated:
I won't comment further on your PHP script because that's not what you asked for.

Thank you, but I would accept any advice at anytime, even if it's off-line.

So, can anyone use my last two posts and give me any advice. Surely what I'm asking for is not impossible (based on the HTML included in this post).

Thanks again, folks - I always appreciate the help I get here, that's why I hang around.

Cheers,
Mike M

Well, I must admit, I'm dead wrong about doing this the way I want using PHP and HTML exclusively. I WILL need some javascript to take care of the form itself and display messages.

I'm off to Barnes & Nobles!

Thanks everyone!
Mike M.
  #5  
Old 22-Sep-2006, 19:08
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 749
admin will become famous soon enough

Re: Variable scope question


That's right, this line in your second example: onsubmit="return validate_reg_form(event)", that's Javascript.

This may not interest you right now but I was in the mood. Here is an example script that will demonstrate how form processing is usually handled by PHP scripts.

Assume that this document is saved as register.php.

PHP Code:

<?php
if( $_POST )
{ // the form was submitted.

    // TODO: Manage (remove) slashes in $_POST values if 'magic_quotes_gpc' is set ON
        
    $error_code = 1; // assume username field is empty already.
    if( isset($_POST['username'])  &&  $_POST['username']=trim($_POST['username']) )
    {
        $error_code = 2; // assume password fields are empty already.
        
        // TODO: verify correct lengths for username & password.
        
        if( isset($_POST['password'])  &&  $_POST['password']=trim($_POST['password']) )
        {
            if( isset($_POST['password2'])  &&  $_POST['password2']=trim($_POST['password2']) )
            {
                $error_code = 4; // assume passwords don't match already
                if( $_POST['password'] === $_POST['password2'] )
                {
                    // SUCCESS! Add script to insert this data into our DB.
                    // ...
                    
                    // Registration was a success, redirect to homepage, activation page or login page.
                    die( header("Location: http://www.example.com/") );     
                }
            }
        }
    }
    else
    {
        $_POST['username'] = null;    
    }
    echo markup_form( $_POST['username'], $error_code );
}
else
{
    echo markup_form();
}
// page markup ends here....
// -------------------------



// FUNCTIONS

/**
 * 
 * @access private
 * @param integer $error_code    The 3 main error codes.  1=>Empty Username Field, 2=>Empty Password Fields (one or both), 4=>Password Mismatch.
 * @return string The error message in <code>&lt;div&gt;</code> tags.
 */
function _markup_form_errors($error_code)
{
    $markup = '<div style="color:red; font-size:smaller;">';
    switch( $error_code )
    {
        case 1: // Empty username field
            $markup .= 'Please fill-in a username.  This data is required.';
            break;
        case 2: // Empty password fields
            $markup .= 'Please fill-in the same password in both the fields below.  This data is required.';
            break;
        case 4: // Password Mismatch
            $markup .= 'The passwords you submitted did not match.  Please try again.';
            break;
        default:
            $markup = null;
    }
    if( $markup )
        $markup .= '</div>';
    return $markup;    
}

/**
 * 
 * 
 * @param string $username The username supplied by the reader. Optional.
 * @param integer $error_code 3 main error codes.  See function <code>_markup_form_errors</code>. Optional.
 * @return string
 */
function markup_form( $username=null, $error_code=null )
{
    // The user supplied 'username' will be displayed in the form if
    // it was submitted but errors also exist e.g. mismatched passwords.
    if( !is_null($username) )
        $username = 'value="' . htmlspecialchars( $username ) . '" ';

    $error_message = ( $error_code ? _markup_form_errors($error_code) : null );
    
    return    "<form name=\"register\" id=\"formRegister\" action=\"$_SERVER[PHP_SELF]\" method=\"post\">\n" .
            "    <p>\n" .
            ( ($error_code & 1) ? "\t\t$error_message\n" : null ) .
            "        <label for=\"inputUsername\">\n" .
            "            <input type=\"text\" name=\"username\" id=\"inputUsername\" size=\"10\" maxlength=\"16\" $username/>\n" .
            "            Username\n" .
            "        </label>\n" .
            "    </p>\n" .
            "    <p>\n" .
            ( ($error_code & 4) ? "\t\t$error_message\n" : null ) .
            ( ($error_code & 2) ? "\t\t$error_message\n" : null ) .
            "        <label for=\"inputPassword\">\n" .
            "            <input type=\"password\" name=\"password\" id=\"inputPassword\" size=\"10\" maxlength=\"16\" />\n" .
            "            Password\n" .
            "        </label>\n" .
            "    </p>\n" .
            "    <p>\n" .
            "        <label for=\"inputPassword2\">\n" .
            "            <input type=\"password\" name=\"password2\" id=\"inputPassword2\" size=\"10\" maxlength=\"16\" />\n" .
            "            Re-type Password\n" .
            "        </label>\n" .
            "    </p>\n" .
            "    <p>\n" .
            "        <input type=\"submit\" name=\"register\" id=\"inputSubmit\" value=\"Register\" />\n" .
            "    </p>\n" .
            "</form>";
}
?>

__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #6  
Old 23-Sep-2006, 15:25
wmmccoy0910's Avatar
wmmccoy0910 wmmccoy0910 is offline
New Member
 
Join Date: Aug 2006
Location: Eastern Virginia, USA
Posts: 21
wmmccoy0910 is on a distinguished road
Talking

Re: Variable scope question


Quote:
Originally Posted by admin
That's right, this line in your second example: onsubmit="return validate_reg_form(event)", that's Javascript.

This may not interest you right now but I was in the mood. Here is an example script that will demonstrate how form processing is usually handled by PHP scripts.

Assume that this document is saved as register.php.
.
.
.
Hey, thanks a lot, not only for not making me feel like moron, but also for providing useful and relevant information. That's what I like about this forum!

I think I have enough information between your post and a couple days of research to "get 'er done!"

Cheers,
Mike M.
 
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
[Tutorial] Pointers in C (Part I) Stack Overflow C Programming Language 1 08-Apr-2005 18:35
Repetition structure problem and question brookeville CPP / C++ Forum 17 29-Oct-2004 17:48
Which Header Files to Use?? BobbyMurcerFan CPP / C++ Forum 8 16-Jun-2004 18:29
my compiler says I have to have a variable at the end of my structure ambeco C Programming Language 10 24-Feb-2004 10:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:19.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.