silly html question

mrhnau

Senior Master
Joined
Aug 5, 2005
Messages
2,269
Reaction score
34
Location
NC
hi guys... been playing around with HTML and such over the past few days. Had some spare time. Installed Apache, etc. anyways, I'm having problems getting data to pass from one html page to another. What I am wanting is the following:

I've got a page that allows you to select one of two radio buttons. upon selection, I want the page to be refreshed (so you can select again) and "you chose 1" (or 2) to be written up top. Before selecting anything, maybe something like "no choice yet". I'm also trying to get it written to a file, but I'm more concerned about the refreshing problem.

Anyways, this is what I have so far.

try.html

<form action="try.html">
<input type="radio" name="test" value="1" onclick="this.form.submit( );">1
<input type="radio" name="test" value="2" onclick="this.form.submit( );">2
</form>


Its something simple, but the searches I've done have not been to helpful so far... I figured someone might know, and save me another 5 hours of searching LOL. All this does is refresh the page.
 
You're not going to be able to do it in straight HTML, you need something that can handle variables and forms. (You should also add a submit button in case someone has javascript disabled.)

That said, page refreshes fine for me as is, I click, it submits. Or am I misreading your question?
 
You're not going to be able to do it in straight HTML, you need something that can handle variables and forms. (You should also add a submit button in case someone has javascript disabled.)
I'm fine using PHP or CGI, as long as it works :) I'm very new to them, as in one day for each. Just installed them on server yesterday :)

That said, page refreshes fine for me as is, I click, it submits. Or am I misreading your question?
It does submit. Problem is, I want the results displayed.
 
It does submit. Problem is, I want the results displayed.

Then you need php or something else, all html can do is set up the form and e-mail it, it can't do anything with the results.

Have a look through this, it demonstrates basic form handling in php. Hopefully it makes sense, if not just ask:

PHP:
<?php
// Internal server variable, returns the name of this file, so you can call it whatever you want.
$phpself =  $_SERVER['PHP_SELF']; 

// Thank you page, redirected to here after processing page. 
// As it is built into the file name, it is a "Get"
if($_GET['do']=="thankyou")
{ 
    echo "Thank you for submitting!";
    return 0;
}

// Has the "Go" button been pushed? 
// The form method was set to POST 
if($_POST['submit']=="Go")
{  
    // Pull the form submitted variables, submitted using POST
    // If globals where turned on you could just use $firstname without doing this, 
    // If that is the case go turn globals off now :D 
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $email = $_POST['email'];
    
    // Create the message
    $to  = $email;
    $subject = 'Form Test';
    
    // Without this it gets sent as plain text, so lets go with html. 
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    // Where the message says it is coming from 
    $headers .= 'From: The Form! <[email protected]>' . "\r\n";
    
    // The message, in this case a html file.
    $message = "
        <html>
            <head>
                <title>Form Test E-mail</title>
            </head>
            <body>
                <p>
                    Hello $firstname, $lastname!
                </p>
            </body>
        </html>
    ";

    // Send the Message!
    mail($to, $subject, $message, $headers);
    
    // Redirect to wherever you want to go after the form has been processed.
    // This also prevents the use of the reload button to resend the form over and over.
    // Redirect requires that nothing has been sent to the browser yet (ex. no "echo"s )
    // Does not have to be the same file, can be anywhere. 
    header("Location: $phpself?do=thankyou");         
}


// The thank you page hasn't been triggered, and the form hasn't been submitted, so draw the form 
echo "
    <html>
        <head>
            <title>Form Handling in PHP</title>
        </head>
        <body>
            <form action='$phpself' name='testform' method='post'>
                <table>
                    <tr>
                        <td>First Name: </td>
                        <td><input type='text' name='firstname' /></td>
                    </tr>
                    <tr>
                        <td>Last Name:</td>
                        <td><input type='text' name='lastname' /></td>
                    </tr>
                    <tr>
                        <td>E-mail: </td>
                        <td><input type='text' name='email' /></td>
                    </tr>
                    <tr>
                        <td><input type='submit' name='submit' value='Go'/></td>
                    </tr>
                </table>
            </form>
        </body>
    </html>

";

?>
 
Andrew's right. HTML lays out the page, you need to submit your values to something else to process.
 
Then you need php or something else, all html can do is set up the form and e-mail it, it can't do anything with the results.

Have a look through this, it demonstrates basic form handling in php. Hopefully it makes sense, if not just ask:

Fantastic :) You da man :) Simple, but demonstrating enough principles that I can take it and run. Thanks man! :)
 
Back
Top