I have a HTML form which sends to a MySQL database via PHP. The form works and saves great until I enter JavaScript to only show certainly fields depending on the users responses. Most of the fields are disabled at the beginning and then enables after certain responses are selected/entered.
Text input fields still save great, however, fields that need a selection of options such as radios and check boxes (which have a mixture of values including Boolean values which I use TinyInt datatypes and text which I use the VarChar datatype) do not save whatsoever.
Does anyone have any idea why? Or have any suggestions on how to find out what is causing the issue? I must stress that the disabling and enabling of inputs at HTML level work fine. The problem is something to do with JavaScript or PHP.
The following code is an EXAMPLE of the structure/format of JavaScript I have been using and the reason for posting this snippet is because it is the most simple one I have:
<script>
$(function () {
$("#wq_7_host_0, #wq_7_host_1").change(function () {
$("#wq_7_hostprov, #wq_7_hostprice, #wq_7_email_0, #wq_7_email_1, #wq_7_email_2").val("").attr("disabled", true);
if ($("#wq_7_host_0").is(":checked")) {
$("#wq_7_hostprov, #wq_7_hostprice, #wq_7_email_0, #wq_7_email_1, #wq_7_email_2").removeAttr("disabled");
$("#wq_7_hostprov").focus();
} else if ($("#wq_7_host_1").is(":checked")) {
$("#wq_7_email_0, #wq_7_email_1, #wq_7_email_2").removeAttr("disabled");
$("#wq_7_email_1").focus();
}
});
});
</script>
My PHP:
<?php
// Database connection string
$dbConn = mysql_connect("info", "info", "info")
or die("Could not connect: " + mysql_error());
mysql_select_db("info", $dbConn)
or die("Could not find database: " + mysql_error());
// create variables from informatin passed from the form
$wq__0_formid = $_POST['wq__0_formid'];
$wq_0_date = $_POST['wq_0_date'];
$wq_0_prona = $_POST['wq_0_prona'];
$wq_1_conna = $_POST['wq_1_conna'];
$wq_1_comna = $_POST['wq_1_comna'];
$wq_1_no = $_POST['wq_1_no'];
ETC
//if no email exists, add the user to the database //Left values are database names and right are variables declared above
mysql_query("INSERT INTO webform
(wq__0_formid, wq_0_date, wq_0_prona, wq_1_conna, wq_1_comna, wq_1_no, wq_1_str, wq_1_tow, wq_1_cit, wq_1_coun, wq_1_pos, wq_1_conno, wq_1_eadd, wq_1_trad, wq_1_tradlo_years, wq_1_tradlo_months, wq_1_tradda, wq_2_web, wq_2_webadd, ETC) VALUES('$wq__0_formid', '$wq_0_date', '$wq_0_prona', '$wq_1_conna', '$wq_1_comna', '$wq_1_no', '$wq_1_str', '$wq_1_tow', '$wq_1_cit', '$wq_1_coun', '$wq_1_pos', '$wq_1_conno', '$wq_1_eadd', '$wq_1_trad', '$wq_1_tradlo_years', '$wq_1_tradlo_months', '$wq_1_tradda', '$wq_2_web', '$wq_2_webadd' ETC)")
or die(mysql_error());
//email customer to let them know login was successful and verify details
$to = "$wq_1_eadd";
$subject = "subject";
$body = "Hi $wq_1_conna,
Message";
if (mail($to, $subject, $body)) {
?>
<script language="javascript" type="text/javascript">
alert('Thanks <?PHP echo $wq_1_conna ?>. We will review the information you have provided and contact you shortly.');
window.location = 'index.html';
</script>
<?php }
else
{
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, get in touch with us via email address.');
window.location = 'index.html';
</script>
<?php
}
?>