PDA

View Full Version : Urgent: Webform to database trouble


Sjimera
07-27-2012, 01:20 AM
Hi,

I have created a webform in DW CS5 and connected it to a database through the insert record server behaviour in php. The form functions fine, but I found all fields need to be filled in order to be able to submit the form. This is quite undesireble. Several fields of the form are supposed to be 'required', but others are not.
Anyone here knows how to resolve this?

edbr
07-27-2012, 02:29 AM
make some conditional statements to end script if certain fields are empty

Sjimera
07-27-2012, 11:53 AM
Thank you for your reaction.
I already figured this would be a possible solution. Unfortunately I'm not PHP savy enough to make such a statement so I guess I was just hoping DW would offer a more simple solution.

edbr
07-28-2012, 02:15 AM
if (empty($_POST['formfield']))
{ echo "you must fill in this field";}

Sjimera
07-28-2012, 03:00 AM
Thank you for giving me the code. I'm afraid though it is the opposite of what I need. I think this typically is the code to insert to make a field a required one?
My form has 18 fields. If any of those is left empty, the form returns an errormessage saying field 'x' has to be filled. But for some fields it should be possible to leave them empty (such as the field 'mobiel' since not everyone does have a mobile phone).
Although I'm not capable to write a PHP script, usually I'm able to find the piece of code responsible for an action I don't want and change it to the action I do want. The code DW generated here keeps me puzzled though.

<?php require_once('Connections/tempDB.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO inschrijven (mailadres, wachtwoord, achternaam, initialen, voornaam, straatnaam, huisnummer, toevoeging, postcode, woonplaats, geslacht, geboortedatum, telefoon, mobiel, trefwoord1, trefwoord2, trefwoord3, trefwoord4, nieuwsbrief) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['mailadres'], "text"),
GetSQLValueString($_POST['wachtwoord'], "text"),
GetSQLValueString($_POST['achternaam'], "text"),
GetSQLValueString($_POST['initialen'], "text"),
GetSQLValueString($_POST['voornaam'], "text"),
GetSQLValueString($_POST['straatnaam'], "text"),
GetSQLValueString($_POST['huisnummer'], "int"),
GetSQLValueString($_POST['toevoeging'], "text"),
GetSQLValueString($_POST['postcode'], "text"),
GetSQLValueString($_POST['woonplaats'], "text"),
GetSQLValueString($_POST['geslacht'], "text"),
GetSQLValueString($_POST['geboortedatum'], "date"),
GetSQLValueString($_POST['telefoon'], "text"),
GetSQLValueString($_POST['mobiel'], "text"),
GetSQLValueString($_POST['trefwoord1'], "text"),
GetSQLValueString($_POST['trefwoord2'], "text"),
GetSQLValueString($_POST['trefwoord3'], "text"),
GetSQLValueString($_POST['trefwoord4'], "text"),
GetSQLValueString(isset($_POST['nieuwsbrief']) ? "true" : "", "defined","1","0"));

mysql_select_db($database_tempDB, $tempDB);
$Result1 = mysql_query($insertSQL, $tempDB) or die(mysql_error());

$insertGoTo = "bevestiging.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_tempDB, $tempDB);
$query_TempInschrijven = "SELECT * FROM inschrijven";
$TempInschrijven = mysql_query($query_TempInschrijven, $tempDB) or die(mysql_error());
$row_TempInschrijven = mysql_fetch_assoc($TempInschrijven);
$totalRows_TempInschrijven = mysql_num_rows($TempInschrijven);
?>