I'm having problems passing variables from one form to another. I'm not even sure if what I want to do is possible. 😕
I have a survey which requires a respondent to enter his email address and answer a lot of questions, although I've altered the code I'm pasting here. The responses are all stored in a MySQL table (genericsurvey).
If the person with the same email address fills out the form a second time, then another form is displayed asking the respondent if he wants to overwrite his previous responses.
This is where I'm stuck
If he selects yes, then the MySQL table should be updated with the new information. Below is the first script (survey.php) which works fine. Beneath that is the second script (overwriteit.php) which is the script that should actually update the table. I can't get the values from survey.php to overwriteit.php and I've tried passing via hidden fields, but it's not working.
I also tried print_r ($_POST) in the second script, but that prints nothing.
Can someone help me out?
SURVEY.PHP
<?php
//If the submit button is pressed
if (isset($_POST['submit'])) {
//Form Validation
//Check for an email address
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",
stripslashes(trim($_POST['email'])))) {
$e = escape_data($_POST['email']);
} else {
$e = FALSE;
echo '<p><font color="red">Please enter a valid email address</font></p>';
}
//Validate Survey Responses
if (isset($_POST['fn_set_1'])) {
$FN1=implode(',',$_POST['fn_set_1']);
} else {
$FN1='';
}
if (isset($_POST['sh_set_1'])) {
$SH1=implode(',',$_POST['sh_set_1']);
} else {
$SH1='';
}
if ($e) {
//Make sure the email address is unique
$query = "SELECT gd_email from genericsurvey WHERE gd_email='$e'";
$result = @mysql_query ($query);
//The email address is unique
if (mysql_num_rows($result) == 0) {
//Add record into database
$query = "INSERT INTO genericsurvey (email, fn_1, sh_1) VALUES ('$e', '$FN1', '$SH1')";
//If the record was successfully added to the database
if ($result = @mysql_query ($query)) {
echo '<p>Thank you for participating in this survey.</p>';
} else {
echo '<p>System error.</p>';
}
//If the email address is not unique
} else {
?>
<p>A respondent with your email address has already submitted a survey.
<p><form action="overwriteit.php" method="POST">
<fieldset><p>Would you like to overwrite it?</p>
<input type="radio" name="overwriteit" value="Yes">Yes
<input type="radio" name="overwriteit" value="No">No<p>
<input type="hidden" name="email" value="<?php $e; ?>">
<input type="hidden" name="fn_set_1" value="<?php $FN1; ?>">
<input type="hidden" name="sh_set_1" value="<?php $SH1; ?>">
<input type="submit" name="submit" value="Submit"></fieldset>
</form>
<?php
}
//If it failed the validation test
} else {
echo '<p><font color="red">Please click "back" and try again.</font></p>';
}
//Display the form
} else {
?>
<?php aboutit(); ?>
<!-- BEGIN SURVEY FORM -->
<form name="genericsurvey" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<fieldset>
Enter your email address* <input type="text" name="email" size="30">
<font color="red">* To ensure uniqueness of respondents</font><br />
<p><?php sh_1(); ?></p>
<p><?php fn_1(); ?></p>
<p align="center"><input type="submit" name="submit" value="Submit">
</fieldset>
</form>
<!-- END SURVEY FORM -->
</div>
</div>
<?php mysql_close(); ?>
<?php
//End of main conditional
}
?>
OVERWRITEIT.PHP
<?php
//Includes the HTML header
include_once ('header.html');
//Includes functions file
include ('functions.php');
//Configuration file for error management
//May need to edit this path
require_once ('../../files/config.inc');
//Connects to the database
//May need to edit this path
require_once ('../../files/mysql_schlupp.php');
if (isset($_POST['submit'])) {
if ($e) {
if ($_POST['overwriteit'] == "Yes") {
$query = "UPDATE generic survey set (gd_email='$e', sh_1='$SH1', fn_1='$FN1') WHERE gd_email='$e'";
//If the record was successfully added to the database
if ($result = @mysql_query ($query)) {
echo '<p>Your previous submission has been overwritten. Thank you for participating in this survey.</p>';
} else {
echo '<p>System error.</p>';
}
} elseif ($_POST['overwriteit'] == "No") {
echo '<p>Your previous submission will <b>not</b> be overwritten. Thank you for participating in this survey.</p>';
}
}
}
mysql_close();
?>