Jump to content

need advice, please read...


StroiX

Recommended Posts

 

When a user visits my page, I would like them to have 2 choices in the form of radio buttons:

 

1) Personal Information

2) Work Information

 

http://www.drychalk.com/_/step1.html


 

In the next step, depending on the choice they made in the previous step, I would like to ask them for the information.  However, regardless of whichever choice they select, I always want to ask them for the "Other Information", please take a look: http://www.drychalk.com/_/step2.html  My question is that how do I use PHP to display the appropriate form fields and hide the others so that I am asking them only about the choice they made in the previous step (and for the "Other Information").  Do I embed all of the form in step2.html in a php file that I use for the "action" attribute in the form of step1.html? or should I do something else... Ultimately I would like to save the information submitted by the user in a MySQL database.

 

http://www.drychalk.com/_/step2.html

 

It would be very much appreciated is someone could provide a working example of this, but as always, just your advice is also much appreciated!!!  =)

 

 

Link to comment
Share on other sites

Try this. this is the code that goes on step 2.

 

<?php

$group1 = $_POST['group1'];

if ($group1 == "personal"){

print<<<HERE

   <form id="form1" name="form1" method="post" action="">
     <p><strong>Personal Information</strong></p>
     <p>Full Name: 
       <label>
   
       <input type="text" name="name" id="name" />
       </label>
     </p>
     <p>Cell Phone: 
       <label>
       <input type="text" name="cellphone" id="cellphone" />
       </label>
     </p>
  
HERE;

} else if ($group1 == "work") {

print<<<HERE
   <form id="form1" name="form1" method="post" action="">
     <p><strong> Work Information</strong></p>
     <p>Company Name: 
       <label>
       <input type="text" name="company" id="company" />
       </label>
     </p>
   
     <p>Work Phone: 
       <label>
   
       <input type="text" name="workphone" id="workphone" />
       </label>
     </p>

HERE;

}
?>

  <hr />
  <p><strong>Other Information</strong></p>
  <p>Your message:</p>
  <p>
    <label>

    <textarea name="message" id="message" cols="45" rows="5"></textarea>
    </label>
  </p>
  <p> </p>
  <p>
    <label>
    <input type="submit" name="submit" id="submit" value="Submit" />
    </label>

  </p>
</form>

Link to comment
Share on other sites

 

Quick question for you:  you have "print<<<HERE" and "HERE;" in the code...  what am I supposed to be replacing this with?  Also...  as of now I have a "process.php" which the form uses to insert the submitted information into a MySQL database.  Should I keep it as it is; separate, or should I include it within the step2.php code.

 

With it being separate right now, I am running into some issues of not having all of the fields filled in since some of them are not being shown.  It is giving me this error when running locally:

 

Notice: Undefined index: company in /Web Directory/ur/process.php on line 14

 

Notice: Undefined index: workphone in /Web Directory/ur/process.php on line 14

1 record added

 

Please refer to the following code from "step2.php" and "process.php".

 

Step2.php:

<?php

$group1 = $_POST['group1'];

if ($group1 == "personal"){

print<<<HERE

   <form id="form1" name="form1" method="post" action="process.php">
     <p><strong>Personal Information</strong></p>
     <p>Full Name: 
       <label>
   
       <input type="text" name="name" id="name" />
       </label>
     </p>
     <p>Cell Phone: 
       <label>
       <input type="text" name="cellphone" id="cellphone" />
       </label>
     </p>
  
HERE;

} else if ($group1 == "work") {

print<<<HERE
   <form id="form1" name="form1" method="post" action="process.php">
     <p><strong> Work Information</strong></p>
     <p>Company Name: 
       <label>
       <input type="text" name="company" id="company" />
       </label>
     </p>
   
     <p>Work Phone: 
       <label>
   
       <input type="text" name="workphone" id="workphone" />
       </label>
     </p>

HERE;

}
?>

  <hr />
  <p><strong>Other Information</strong></p>
  <p>Your message:</p>
  <p>
    <label>

    <textarea name="message" id="message" cols="45" rows="5"></textarea>
    </label>
  </p>
  <p> </p>
  <p>
    <label>
    <input type="submit" name="submit" id="submit" value="Submit" />
    </label>

  </p>
</form>

 

process.php

<?php

// 1. Create a database connection
$connection = mysql_connect("x", "x", "x");
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db('ur', $connection);

mysql_query("INSERT INTO personal (name, cellphone) VALUES ('$_POST[name]', '$_POST[cellphone]')");

mysql_query("INSERT INTO work (company, workphone) VALUES ('$_POST[company]', '$_POST[workphone]')");

mysql_query("INSERT INTO message (message) VALUES ('$_POST[message]')");

echo "1 record added";

mysql_close($connection)
?>

 

 

http://www.drychalk.com/_/ur/step1.html

 

Thank you once again.

 

 

Link to comment
Share on other sites

The error is probably because it's trying to do BOTH inserts when really you only need one. So you need to check which form was submitted, then add the information to the necessary table.

 

<?php

// 1. Create a database connection
$connection = mysql_connect("x", "x", "x");
if (!$connection) {
	die("Database connection failed: " . mysql_error());
}

// 2. Select a database to use
$db_select = mysql_select_db('ur', $connection);


if (isset($_POST['name']) && isset($_POST['cellphone'])){
   mysql_query("INSERT INTO personal (name, cellphone) VALUES ('{$_POST['name']}', '{$_POST['cellphone']}')");
} else if (isset($_POST['company']) && isset($_POST['workphone'])){
   mysql_query("INSERT INTO work (company, workphone) VALUES ('{$_POST['company']}', '{$_POST['workphone']}')");
}

mysql_query("INSERT INTO message (message) VALUES ('{$_POST['message']}')");

echo "1 record added";

mysql_close($connection)
?>

 

I would also suggest using mysql_real_escape_string() on all variables before using them in a query, especially POST variables.

Link to comment
Share on other sites

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.