Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-17-2009, 07:49 PM   PM User | #1
Sasso
New to the CF scene

 
Join Date: Oct 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Sasso is an unknown quantity at this point
PHP Form with internal pages/id

Hi anyone,

First of all congratulations on this wonderful forum!

I’m looking for a solution for a multipage php form . I’d to know how to pass vars from a page to the other. I’ve tried using hidden fields and sessions but don’t work. My problem is that pages are just ids inside in a single page.

I’d to make in any page that links to the next and in the last page a button to send the form (it’s easy but I cannot get vars from previous pages, so it gives errors for blank fields.

This is the code (I’ve dumb down it to understand better the problem):

PHP Code:
<?
$id
=$_GET[id];

// ################### CONFIG ###################

$class_txt "text";
$class_inputbutton "inputButton";
$class_inputline "inputLine";
$class_inputfield "inputField";
$style_inputline "width:355px;";
$style_inputfield "width:355px;";

// email
$target_address "info@email.it";
$email_subject "Oggetto";

// error messages
$err_name =    "name";
$err_email "e-mail";
$err_tit "title";

// misc text
$msg_date "Date";
$msg_name "Name";
$msg_email "Email:";
$msg_tit "Title ";

$txt_send "Send";
$txt_mandatory "required";

$msg_indent 11;

// messages
$txt_thankyou "<div class='$class_txt'><h2>Thank you!</h2></div>";
$txt_error "<div style='color: #cc3300' class='$class_txt'><h2>Error! Check the follow fields:</h2>{errors}</div>";    // {errors} is replaced by the errors that occurred

// ################ END  CONFIG #################

function spaces($num$fill=" "){
    
$foo="";
    for (
$i=0$i<$num$i++) $foo.=$fill;
    return 
$foo;
}

function 
isValidEmail($addr){
    if(
eregi("^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,4}$"$addr))
        return 
true;
    else
        return 
false;
}


// start form evaluation
$error="foo";
if (
$_REQUEST['do']=="send"){
    
$error=false;
    if (
$_REQUEST['name']==""$error.="&raquo; $err_name<br>";                     
    if (
$_REQUEST['tit']==""$error.="&raquo; $err_tit<br>";     


    if (!
isValidEmail($_REQUEST['email'])) $error.="&raquo; $err_email<br>";
    if (
$error===false){
    
        
$message="$msg_date:".spaces($msg_indent-strlen($msg_date)).date("d M Y, H:i"time());
        if (
$_REQUEST['name']) $message.="\n$msg_name:".spaces($msg_indent-strlen($msg_name)).$_REQUEST['name'];
        if (
$_REQUEST['tit']) $message.="\n$msg_tit:".spaces($msg_indent-strlen($msg_tit)).$_REQUEST['tit'];        
      
$message.="\n$msg_email:".spaces($msg_indent-strlen($msg_email))."mailto:".$_REQUEST['email'];
        
$message.="\n\n$msg_request:\n\n".$_REQUEST['message'];
        
        
mail($target_address$email_subject$message"From: ".$_REQUEST['email']);
        echo 
$txt_thankyou;
        
    }else if (
$error!==false$error=str_replace("{errors}"$error$txt_error);

}

if (
$error!==false){

    if(
$error!="foo") echo $error;
    
    
// form
    
echo "<script language='JavaScript' type='text/JavaScript'>\n";
    echo 
"window.onload = function(){ document.form1.firma.focus(); }\n";
    echo 
"</script>\n";    

    echo 
"<form name='form1' method='post' action=''>\n";

    echo 
"<h2>FORM</h2>\n";            

    if (
$id == '1')  {     
    echo 
"<table  border='0' class='txt'>\n";
    echo 
"<tr><td class='$class_txt'>$msg_name *</td>\n";
    echo 
"<td><input name='name' type='text' class='$class_inputline' style='$style_inputline' value='".$_REQUEST['name']."'>\n";
    echo 
"</td></tr>\n";    
    echo 
"</table>\n";   
  }    
  
    else if (
$id == '2')  {     
    echo 
"<table  border='0' class='txt'>\n";
    echo 
"<tr><td class='$class_txt'>$msg_tit *</td>\n";
    echo 
"<td><input name='tit' type='text' class='$class_inputline' style='$style_inputline' value='".$_REQUEST['tit']."'>\n";
    echo 
"</td></tr>\n";    
    echo 
"</table>\n";   
  }    
    
  else {  
    echo 
"<table  border='0' class='txt'>\n";
    echo 
"<tr><td class='$class_txt'>$msg_email *</td>\n";
    echo 
"<td><input name='email' type='text' class='$class_inputline' style='$style_inputline' value='".$_REQUEST['email']."'>\n";
    echo 
"</td></tr>\n";
    echo 
"<tr><td><input name='Submit' type='Submit' class='$class_inputbutton' value='$txt_send'>\n";
    echo 
"<input name='do' type='hidden' id='do' value='send'></td>\n";
    echo 
"</tr>\n";       
    echo 
"</table>\n";    
}    

    echo 
"</form>\n";

}

?>
PS: Sorry for the language but im from Italian
Sasso is offline   Reply With Quote
Old 10-17-2009, 10:32 PM   PM User | #2
powerfear
New to the CF scene

 
Join Date: Oct 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
powerfear is an unknown quantity at this point
Here an example

PHP Code:
<?php
if (isset($_POST['submit'])) {
$var $_POST['var'];
echo 
$var;
//other codes you want
} else {
?>
<title>Example</title>
<center><h1>Example</h1></center><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<center><h3>Example var:</h3>
<input type="text" name="var" maxlength="500"><br><br>
<input type="submit" name="submit" value="Submit">
</center>
</form>
<?php
}
?>
Basicly you use a normal html form and use $_POST['inputname'] to get the var you use $_SERVER['PHP_SELF'] to post to your own page and isset to know if you display the form or run the code.
powerfear is offline   Reply With Quote
Old 10-17-2009, 11:14 PM   PM User | #3
barkermn01
Regular Coder

 
Join Date: Nov 2007
Location: Leeds, UK
Posts: 514
Thanks: 24
Thanked 19 Times in 19 Posts
barkermn01 can only hope to improve
Quote:
Originally Posted by powerfear View Post
Here an example

PHP Code:
<?php
if (isset($_POST['submit'])) {
$var $_POST['var'];
echo 
$var;
//other codes you want
} else {
?>
<title>Example</title>
<center><h1>Example</h1></center><br>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<center><h3>Example var:</h3>
<input type="text" name="var" maxlength="500"><br><br>
<input type="submit" name="submit" value="Submit">
</center>
</form>
<?php
}
?>
Basicly you use a normal html form and use $_POST['inputname'] to get the var you use $_SERVER['PHP_SELF'] to post to your own page and isset to know if you display the form or run the code.
Just a bit more ::: $_(method of form)
E.G
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
means its in $_POST

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
means its in $_GET

OR the bad way
$_REQUEST
Is both $_GET and $_POST
__________________
Working towards a Internet where we don't have website just browser applications Kill the Hyper-link and say hello to 3D Games in the browser :)
barkermn01 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:33 AM.


Advertisement
Log in to turn off these ads.