Jump to content

Recommended Posts

Hi.   I am new to PHP programming and I am trying to get a multi form web site to work.    I was wondering if I could receive some help.   My first .php file looks like this. 

<?php
$connect=mysql_connect('server','userid','passwd') or die("Unable to Connect");
@mysql_select_db('table') or die("Could not open the db");
session_start();
$first = $_POST['first'];
$last = $_POST['last'];
switch ($_POST['hwphase']) {
case "HAS":
   $url = "form2.php?$first=first&$last=last";
   // Or this could be a relative path like:  $url="has_survey.php";
break;
case "HD":
   $url = "form3?$first=first&$last=last.php";
break;
case "HR":
  $url = "form4?$first=first&$last=last.php";
}
header("Location: $url");
?>
 

 

My problem is with the next .php file.    I am unable to "get" the first and last name.   This is what I have:

 

<html>
<body>
<?php
$connect=mysql_connect('server','userid','passwd') or die("Unable to Connect");
@mysql_select_db('table') or die("Could not open the db");
session_start();
$_SERVER['first'] = $_GET['first'];
$_SERVER['last'] = $_GET['last'];
 
?>
<form method="post" action="form3.php">
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
<br>
hidden <br>
<input type="text" name="first"  value="<?php echo $_GET['first']; ?>"><br>
<input type="text" name="last"  value="<?php echo $_GET['last']; ?>"><br>
<br>
<input type="submit" value="Go To Step 3">
</form>
</body>
</html>
I greatly appreciate your help.

looks like you have your parameters and values backwards, and you have the file extension in the wrong place...

 

$url = "form3?$first=first&$last=last.php";
should be

 

$url = "form3.php?first=$first&last=$last";
same thing with your other $url assignments.

On a side note.. while your method of carrying over previous form data to new form (for what I assume is a multi-step process) "works", perhaps you should consider instead carrying over the previous data with session variables instead of outputting them as hidden form fields. This will make them more easily accessible, especially concerning the user going back a step in the form.

Josh.   Thank you.  That was a typo on my part.   Yes, I just fixed those.   But I am still not able to pull the "first" and "last" in to the hidden fields of the form2.php, form3.php and form4.php.    This is where my issue is..  All I get is a blank textbox.  I know I am missing something.  

In addition, there are a number of other things off with your code.

 

1) You have database connection code in both your scripts. In at least the first one, you aren't using it at all, based on the fact that you do a header redirect. You probably aren't even using it in the 2nd file either, but not enough context to know. So.. why do you have that in there? You shouldn't make your script connect to a database if you aren't going to actually do anything with it in the script. On that note...

 

2) You shouldn't have your database connection info in a publicly accessible script at all. If your server for whatever reason fails to parse the php in the file, it's going to output the whole file - including your php code - as plain text to the user. This can happen for many reasons..like improperly changing the mime type config for what server should do with files of certain extension, or improperly updating php engine on your server, etc.. At a minimum, you should store your db connection stuff outside of the public web directory structure, and then include it on the pages (that actually use it).

 

3) While we're still on the subject of database code.. the mysql_xxx functions are deprecated. At a minimum, you should update your db syntax to use the mysqli_xxx functions (notice the "i"), though I personally like the PDO functions better. Not that you're actually using your db in these scripts.. but I'm mentioning it anyway since I figure you probably are in fact using it on some final form submission script.

 

4) You have session_start in both your scripts, but don't appear to be utilizing sessions. Now, I did suggest in my previous post that you should utilize session vars instead of passing form data along to the next step with url params and hidden fields, so if you want to move forward with that, then they should stay there. However, in your 2nd script, you have it placed at a point in your script that's after you already started outputting stuff to the browser. If you had your error level settings set properly for development, php should have given you a "headers already sent" warning about this. If you do not want to move forward with using session vars to pass data back and forth, you should remove those session_start lines.

 

5) In your 2nd script, you have 2 lines that look like this: $_SERVER['first'] = $_GET['first']; I don't even know what you are attempting to do here.. My best guess is maybe this was your attempt to set a session variable? $_SERVER is a superglobal that contains information about the request to the server/script. In practice, $_SERVER should be a read-only variable. Also, any data in that variable is only within the scope of the currently executing script. IOW this is not what you use for sessions. For sessions, you use $_SESSION.

Josh.   Thank you.  That was a typo on my part.   Yes, I just fixed those.   But I am still not able to pull the "first" and "last" in to the hidden fields of the form2.php, form3.php and form4.php.    This is where my issue is..  All I get is a blank textbox.  I know I am missing something.

Post your updated code

John.    I would like to do the session_start route.  But on my first form, I need to have the individuals select the route they are going to take from the drop down.   With doing this, I was instructed to go this route:

 

This is the form
 

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Testing HAS Form theory</title>
</head>

<body>
<form method="POST" action="formPHASE2.php"> 
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
<select name="hwphase" id="hwphase">
     <option selected>- - Select - -</option>
     <option value="HAS">HAS</option>
     <option value="HD">HD</option>
     <option value="HR">HR</option>
<input type="submit" value="Go To Step 2">
</form>

</body>
</html>

 

formPHASE2.php

<?php

$first = $_POST['first'];
$last = $_POST['last'];

switch ($_POST['hwphase']) {

case "HAS":
   $url = "http://mobility.web.alcatel-lucent.com/~norab/test1/form2.php?first=$first&last=$last";
   // Or this could be a relative path like:  $url="has_survey.php";
break;
case "HD":
   $url = "http://mobility.web.alcatel-lucent.com/~norab/test1/hd.php?first=$first&last=$last";
break;
case "HR":
  $url = "http://mobility.web.alcatel-lucent.com/~norab/test1/hr.php?first=$first&last=$last";
}
header("Location: $url");
?>

 

The form2.php looks like this:

<html>
<body>

<?php

session_start();  

$_SESSION['first'] = $first;
$_SESSION['last'] = $last;

?>

<form method="post" action="form3.php">
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
<br>
hidden <br>
<input type="text" name="first"  value="<?php echo $_GET['first']; ?>"><br>
<input type="text" name="last"  value="<?php echo $_GET['last']; ?>"><br>
<br>
<input type="submit" value="Go To Step 3">
</form>

</body>
</html>

 

In keeping with the session_start process, what would you suggest for doing specific url for the drop down?   Should I be doing the switch case?

 

My multi forms are for a company survey and I have about 10 questions.   Each form has to be on a separate page.   I greatly appreciate your input.   Thanks!

You need to be assigning the posted data to the session in formPHASE2.php, for example:

session_start();  
$_SESSION['first'] = $_POST['first'];
$_SESSION['last'] = $_POST['last'];
There is no need to include it in your URL's for your redirect, so just remove the parameters from there.

 

On the next page, you then need to read the data back out of the session, for example:

session_start();
$first = $_SESSION['first'];
$last = $_SESSION['last'];

echo 'First: '.$first.'; Last: '.$last;

Hi.  One more issue and I hope I am done.   I have a "comments" field in my form:

 

<tr>
    <td width="99%" height="30" colspan="2" style="font-family: Calibri; font-size: 14pt">
    If the last checklist review is not completed or in redirect, please provide reason.<br>
     <input type=text size=100 NAME="comment1"></td>
    </td>

</tr>

 

When I test the form, I put in the text box "This is a text of the comment field for section 1".

 

My PHP data:

 

<?php

session_start();

$name = $_SESSION['name'];
$cslogin = $_SESSION['cslogin'];
$domain = $_SESSION['domain'];
$project = $_SESSION['project'];
$hwphase = $_SESSION['hwphase'];
$ppmid = $_SESSION['ppmid'];
$survey = $_SESSION['survey'];
$month = $_SESSION['month'];
$q1 = $_SESSION['q1'];
$comment1 = $_SESSION['comment1'];

echo 'Name: '.$name.'; Month: '.$month;
?>
<br>
     <input type="text" size=100 name="name" value="<?php echo $name ?>"><br>
     <input type="text" size=100 name="cslogin" value="<?php echo $cslogin ?>"><br>
     <input type="text" size=100 name="domain" value="<?php echo $domain ?>"><br>
     <input type="text" size=100 name="project" value="<?php echo $project ?>"><br>
     <input type="text" size=100 name="hwphase" value="<?php echo $hwphase ?>"><br>
     <input type="text" size=100 name="ppmid" value="<?php echo $ppmid ?>"><br>
     <input type="text" size=100 name="survey" value="<?php echo $survey ?>"><br>
     <input type="text" size=100 name="month" value="<?php echo $month ?>"><br>
     <input type="text" size=100 name="q1" value="<?php echo $q1 ?>"><br>
     <input type="text" size=100 name="comment1" value="<?php echo $comment1?>"><br>

 

For some reason it prints out only the word "test" for Comments.   Everything else echo fine.   Is there something I am missing?

 

I greatly appreciate your help.

 

Thanks!!

 

 

at face value, this looks okay. Where are you setting $_SESSION['comment1']; I assume on a previous page? Are you sure you didn't typo the name and reference a different form field when you set it? Because it's not some truncation of the value you entered in.. it's a completely different value...

Hi.   Even if I change the field name to something different, I still get the word "Test" as a value.    I just can't figure out what is wrong.   

I am setting the $_SESSION['comment1'] in the next page.   Here is the code.

 

Question 1 page:

 

<tr>
    <td width="99%" height="30" colspan="2" style="font-family: Calibri; font-size: 14pt">
    If the last checklist review is not completed or in redirect, please provide reason.<br>
     <input type=text size=100 NAME="comment1"></td>
    </td>
 

Goes to <form method="post" action="hasq2.php">
 

Here is the code from hasq2.php

 

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Wireless Hardware Quality Practice Assessment Survey - Question 2</title>
</head>
<body>

<?php

session_start();

$name = $_SESSION['name'];
$cslogin = $_SESSION['cslogin'];
$domain = $_SESSION['domain'];
$project = $_SESSION['project'];
$hwphase = $_SESSION['hwphase'];
$ppmid = $_SESSION['ppmid'];
$survey = $_SESSION['survey'];
$month = $_SESSION['month'];
$q1 = $_SESSION['q1'];
$comment1 = $_SESSION['comment1'];

echo 'Name: '.$name.'; Month: '.$month;

?>
<br>
     <input type="text" size=100 name="name" value="<?php echo $name ?>"><br>
     <input type="text" size=100 name="cslogin" value="<?php echo $cslogin ?>"><br>
     <input type="text" size=100 name="domain" value="<?php echo $domain ?>"><br>
     <input type="text" size=100 name="project" value="<?php echo $project ?>"><br>
     <input type="text" size=100 name="hwphase" value="<?php echo $hwphase ?>"><br>
     <input type="text" size=100 name="ppmid" value="<?php echo $ppmid ?>"><br>
     <input type="text" size=100 name="survey" value="<?php echo $survey ?>"><br>
     <input type="text" size=100 name="month" value="<?php echo $month ?>"><br>
     <input type="text" size=100 name="q1" value="<?php echo $q1 ?>"><br>
     <input type="text" size=100 name="comment1" value="<?php echo $comment1 ?>"><br>
</body>
</html>

 

I don't set the variable in the question 1 file.   Should I set the variable there too?

 

Thank you for your help.

Ok.   If I change the code on the hasq2.php as follows:

 

$_SESSION['month'] = $_POST['month'];
$_SESSION['q1'] = $_POST['q1'];
$_SESSION['comment1'] = $_POST['comment1'];
 

I am able to see the "Month", Question 1" and Comment 1.   But, all the other fields are lost.  "Name, CSLogin, PPMID" and so on.

 

How do I get the reamaining fields to carry on to the next question?

 

This php is not reading the POSTed data from the previous form.

$name = $_SESSION['name'];
$cslogin = $_SESSION['cslogin'];
$domain = $_SESSION['domain'];
$project = $_SESSION['project'];
$hwphase = $_SESSION['hwphase'];
$ppmid = $_SESSION['ppmid'];
$survey = $_SESSION['survey'];
 

Should I do a $_SESSION['name'] = $_POST['name']; to reestablish it or will this just cause problems?

 

I greatly appreciate your help.

 

Thanks!!
 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.