Posting form results into database

12 posts by 3 authors in: Forums > CMS Builder
Last Post: June 23, 2010   (RSS)

By MRI - June 18, 2010

For the most part I have this working.. only issue im having is with check boxes and drop down lists. Ill enter all info in a form and hit submit, when I check it in the CMS everything shows except for the item that required me to check a box or select from drop down list.

any help would be great!

also the script you guys gave me a while back for this displays a message after submitting... is there a way for me to direct to a confirmation page instead?

// display thanks message and clear form
$errorsAndAlerts = "Thank you";
$_REQUEST = array();
}

Re: [MRI] Posting form results into database

By Chris - June 18, 2010 - edited: June 18, 2010

Hi MRI,

Can you please post the complete PHP source code for the page you're working on?
All the best,
Chris

Re: [chris] Posting form results into database

By MRI - June 18, 2010

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "XXXX/admin/lib/init.php";

// submit form
if (@$_REQUEST['submit']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['first_name']) { $errorsAndAlerts .= "Please specify First Name!<br/>\n"; }
if (!@$_REQUEST['last_name']) { $errorsAndAlerts .= "Please specify Last Name!<br/>\n"; }
if (!@$_REQUEST['email_address']) { $errorsAndAlerts .= "Please specify Email Address!<br/>\n"; }

// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}view_sign_up_list` SET
first_name = '".mysql_real_escape_string( $_REQUEST['first_name'] )."',
last_name = '".mysql_real_escape_string( $_REQUEST['last_name'] )."',
club_number = '".mysql_real_escape_string( $_REQUEST['club_number'] )."',
email_address = '".mysql_real_escape_string( $_REQUEST['email_address'] )."',
yes_emails = '".mysql_real_escape_string( $_REQUEST['yes_emails'] )."',
mobile_phone_number = '".mysql_real_escape_string( $_REQUEST['mobile_phone_number'] )."',
cell_phone_carrier = '".mysql_real_escape_string( $_REQUEST['cell_phone_carrier'] )."',
yes_text = '".mysql_real_escape_string( $_REQUEST['yes_text'] )."',

createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

// display thanks message and clear form
$errorsAndAlerts = "Thank you";
$_REQUEST = array();
}

}

?>
<?php

require_once "/XXXX/admin/lib/viewer_functions.php";

list($email_text_form_pageRecords, $email_text_form_pageMetaData) = getRecords(array(
'tableName' => 'email_text_form_page',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$email_text_form_pageRecord = @$email_text_form_pageRecords[0]; // get first record



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
XXXXXXXXX
</head>

<body onload="MM_preloadImages('../images/club/menu_dwn_r3_c1.jpg','../images/club/menu_dwn_r5_c1.jpg','../images/club/menu_dwn_r7_c1.jpg','../images/formpages/submit_ov.gif')">
<div id="formbox">

<form method="post" action="">
<input type="hidden" name="submit" value="1" />
<?php if (@$errorsAndAlerts): ?>
<div style="color:FFFFFF; font-weight: bold; font-size: 17px; font-family:Arial, Helvetica, sans-serif"><br/>
<?php echo $errorsAndAlerts; ?><br/><br/>
</div>
<?php endif ?>
<table width="645" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px">
*<b>First Name:</b><br />
<label>
<input name="first_name" type="text" id="textbox" size="40" value="<?php echo htmlspecialchars(@$_REQUEST['first_name']) ?>"/>
</label>
</td>
<td align="left" valign="top" style="padding:0px 15px 20px 0px">
*<b>Last Name:</b><br />
<label>
<input name="last_name" type="text" id="textbox" size="40" value="<?php echo htmlspecialchars(@$_REQUEST['last_name']) ?>" />
</label></td>
</tr>
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px" colspan="2">

</td>

</tr>
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px" colspan="2">
<b>Card Number:</b><br />
<label>
<input name="club_number" type="text" id="textbox" size="40" value="<?php echo htmlspecialchars(@$_REQUEST['club_number']) ?>" />
</label></td>

</tr>
<tr>
<td align="left" valign="top" style="padding:15px 0px 20px 0px" colspan="2">
<div class="purpleheader"> EMAIL SIGN-UP:</div></td>

</tr>
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px" colspan="2">
*<b>Email Address:</b><br />
<label>
<input name="email_address" type="text" id="textbox" size="40" value="<?php echo htmlspecialchars(@$_REQUEST['email_address']) ?>" />
</label></td>

</tr>
<tr>
<td align="left" valign="top" colspan="2" style="padding:0px 15px 30px 0px"><label>
<input name="yes_emails" type="checkbox" id="Yes" checked="checked" value="<?php echo htmlspecialchars(@$_REQUEST['yes_emails']) ?>" />
</label>
YES! I would like to receive emails!</tr>
<tr>
<td align="left" valign="top" style="padding:15px 0px 20px 0px" colspan="2">
<div class="purpleheader">TEXT MESSAGING SIGN-UP:</div></td>

</tr>
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px" colspan="2">
*<b>Mobile Phone Number (XXX-XXX-XXXX):</b><br />
<label>
<input name="mobile_phone_number" type="text" id="textbox" size="40" value="<?php echo htmlspecialchars(@$_REQUEST['mobile_phone_number']) ?>" />
</label></td>

</tr>
<tr>
<td align="left" valign="top" style="padding:0px 15px 20px 0px" colspan="2">
<b>Cell Phone Carrier:</b><br />
<select name="cell_phone_carrier" id="dropdown" >
<option selected="selected">Select One</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Verizon</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Nextel</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Sprint</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">T-Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Boost Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Midwest</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Suncom</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Alltel</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">AT&amp;T</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Virgin Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">U.S. Cellular</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Cincinnati Bell</option>
</select></td>

</tr>
<tr>
<td align="left" valign="top" colspan="2" style="padding:0px 15px 25px 0px"><label>
<input name="yes_text" type="checkbox" id="Yes" checked="checked" value="<?php echo htmlspecialchars(@$_REQUEST['yes_text']) ?>" />
</label>
YES! I would like to receive text messages!</tr>
<tr>
<td align="left" valign="top" colspan="2" style="padding:0px 15px 30px 0px">
<input type="image" name="add" value="Submit" src="../images/formpages/submit.gif" />
<br /><br />

</tr>

</table>
</form>
</div>


</body>
</html>

Re: [MRI] Posting form results into database

By Jason - June 21, 2010

Hi,

Are you finding that the drop down and check box are putting empty values into CMS Builder?

I noticed that the values you're assigning to the drop down are coming from the $_REQUEST variable:

<select name="cell_phone_carrier" id="dropdown" >
<option selected="selected">Select One</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Verizon</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Nextel</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Sprint</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">T-Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Boost Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Midwest</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Suncom</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Alltel</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">AT&amp;T</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Virgin Mobile</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">U.S. Cellular</option>
<option value="<?php echo htmlspecialchars(@$_REQUEST['cell_phone_carrier']) ?>">Cincinnati Bell</option>
</select>


The $_REQUEST variable is only populated when a form is submitted. This means that if your page has not been submitted, all of these options will have blank values. Another problem would be that if you did submit the form, all of the options would have the same value (the one that was selected.

Each option should have a separate value, something like:

<select name="cell_phone_carrier" id="dropdown" >
<option selected="selected">Select One</option>
<option value="Verizon">Verizon</option>
<option value="Nextel">Nextel</option>
.
.
.
</select>


There's a similar problem with the check box
<input name="yes_text" type="checkbox" id="Yes" checked="checked" value="<?php echo htmlspecialchars(@$_REQUEST['yes_text']) ?>" />

This box will have an empty value if the form has never been submitted. Try something like this:

<input name="yes_text" type="checkbox" id="Yes" checked="checked" value="1" />

Give this a try and let me know how that works for you.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Posting form results into database

By MRI - June 21, 2010

ill give that a shot...thanks

Re: [MRI] Posting form results into database

By MRI - June 21, 2010

ok it worked...kind of..

Everything enters into CMSBuilder fine now.. but if I uncheck a box... and submit.. I get this error

Notice: Undefined index: yes_emails in XXXXX/club/signup.php on line 24 Notice: Undefined index: yes_text in XXXXX/club/signup.php on line 27


--------

heres where those errors are:
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}view_sign_up_list` SET
first_name = '".mysql_real_escape_string( $_REQUEST['first_name'] )."',
last_name = '".mysql_real_escape_string( $_REQUEST['last_name'] )."',
crown_club_number = '".mysql_real_escape_string( $_REQUEST['club_number'] )."',
email_address = '".mysql_real_escape_string( $_REQUEST['email_address'] )."',
yes_emails = '".mysql_real_escape_string( $_REQUEST['yes_emails'] )."',
mobile_phone_number = '".mysql_real_escape_string( $_REQUEST['mobile_phone_number'] )."',
cell_phone_carrier = '".mysql_real_escape_string( $_REQUEST['cell_phone_carrier'] )."',
yes_text = '".mysql_real_escape_string( $_REQUEST['yes_text'] )."',

createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

Re: [MRI] Posting form results into database

By Chris - June 21, 2010

Hi MRI,

I want to amend Jason's post to suggest you use selectedIf() and checkedIf() to pass request variables forward:

<option value="Verizon" <?php selectedIf(@$_REQUEST['cell_phone_carrier'], 'Verizon') ?>>Verizon</option>

and:

<input name="yes_text" type="checkbox" value="1" <?php checkedIf(@$_REQUEST['yes_text'], '1') ?> />

Regarding your error, you'll need to change your MySQL query. Checkbox fields don't send anything when unchecked. Try changing this:

yes_emails = '".mysql_real_escape_string( $_REQUEST['yes_emails'] )."',

to this:

yes_emails = '".(@$_REQUEST['yes_emails'] ? '1' : '0')."',

And do the same for any other checkboxes.

I hope this helps. Please let me know if you have any questions!
All the best,
Chris

Re: [chris] Posting form results into database

By MRI - June 21, 2010

perfect!

now if only I can figure out how to have it redirect to another page after submitting as opposed to just displaying "Thank You"


<?php
require_once "XXXX/admin/lib/init.php";

// submit form
if (@$_REQUEST['submit']) {

// error checking
$errorsAndAlerts = "";
if (!@$_REQUEST['first_name']) { $errorsAndAlerts .= "Please specify First Name!<br/>\n"; }
if (!@$_REQUEST['last_name']) { $errorsAndAlerts .= "Please specify Last Name!<br/>\n"; }
if (!@$_REQUEST['email_address']) { $errorsAndAlerts .= "Please specify Email Address!<br/>\n"; }

// turn off strict mysql error checking for: STRICT_ALL_TABLES
mysqlStrictMode(false); // disable Mysql strict errors for when a field isn't defined below (can be caused when fields are added later)

// add record
if (!@$errorsAndAlerts) {
mysql_query("INSERT INTO `{$TABLE_PREFIX}view_sign_up_list` SET
first_name = '".mysql_real_escape_string( $_REQUEST['first_name'] )."',
last_name = '".mysql_real_escape_string( $_REQUEST['last_name'] )."',
club_number = '".mysql_real_escape_string( $_REQUEST['club_number'] )."',
email_address = '".mysql_real_escape_string( $_REQUEST['email_address'] )."',
yes_emails = '".mysql_real_escape_string( $_REQUEST['yes_emails'] )."',
mobile_phone_number = '".mysql_real_escape_string( $_REQUEST['mobile_phone_number'] )."',
cell_phone_carrier = '".mysql_real_escape_string( $_REQUEST['cell_phone_carrier'] )."',
yes_text = '".mysql_real_escape_string( $_REQUEST['yes_text'] )."',

createdDate = NOW(),
updatedDate = NOW(),
createdByUserNum = '0',
updatedByUserNum = '0'")
or die("MySQL Error Creating Record:<br/>\n". htmlspecialchars(mysql_error()) . "\n");
$recordNum = mysql_insert_id();

// display thanks message and clear form
$errorsAndAlerts = "Thank you";
$_REQUEST = array();
}

}

?>

Re: [MRI] Posting form results into database

By Chris - June 21, 2010

Hi MRI,

Okay, replace this:

// display thanks message and clear form
$errorsAndAlerts = "Thank you";
$_REQUEST = array();


...with this:

// redirect to thank you page
header('Location: thankyou.php');
exit;


Please note that for this to work, you can't have anything output before it. That includes extra spaces or newlines at the top of your script before the <?php

I hope this helps! Please let me know if you have any questions.
All the best,
Chris