Web Hosting Talk







View Full Version : PHP Omitting blank fields on send?


kayz
06-09-2009, 04:03 PM
Hi guys i have a pretty large working form, when the user dosent fill in a field and submits the form i still receive the field name(s) with a blank entry.. for a large form its a lot to look at when it arrives to see what has been filled in and what hasnt.

Because it is a large form, some areas will be filled in and others will be ignored. What i want is the form to send me only those fields that has been filled in.


This is my "example" working processor, i know it requires a simple if statement, but ive tried many without much luck, your help would be much appreciated.

Cheers





<?php

session_start();

$receiver = '******@hotmail.com';
$subject = 'Website - Feedback';
$headers = 'info@************';


$err_msg = '';
if((!$name)){
$err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />';

if(!$name){
$err_msg .= "* Your Name<br />";
}
include 'index.php';

exit();
}


$arr= array();

$arr[0] = "\n\n Name: ";
$arr[1] = $_SESSION['name'];

$arr[2] = "\n\n email_address: ";
$arr[3] = $_SESSION['email_address'];


$arr[4] = "\n\n membership_type: ";
$arr[5] = $_SESSION['membership_type'];


$arr[6] = "\n\n terms_and_conditions: ";
$arr[7] = $_SESSION['terms_and_conditions'];


$arr[8] = "\n\n name_on_card: ";
$arr[9] = $_POST['name_on_card'];



$arr[10] = "\n\n credit_card_number: ";
$arr[11] = $_POST['credit_card_number'];


$arr[12] = "\n\n credit_card_expiration: ";
$arr[13] = $_POST['credit_card_expiration_date'];


$content = ($arr[0] . $arr[1] . $arr[2] . $arr[3] . $arr[4] . $arr[5] . $arr[6] . $arr[7] . $arr[8] . $arr[9] . $arr[10] . $arr[11] . $arr[12] . $arr[13]);

mail("$receiver", "$subject!", "$content", "From: $headers");
header("Location: thankyou.html");

?>

HivelocityDD
06-09-2009, 06:59 PM
You can try some thing like this

<?php

$mypostData = array();

foreach($_POST as $key=>$value) {
if($value != "") {
$mypostData[$key] = $value;
}
}

print_r($mypostData); // This will show all the fields that have values inside it


Let me know how it goes please

csparks
06-09-2009, 07:25 PM
I would have done this like this:


//Set the field labels for each form element here
$arrNames = array(
'field_name' = > 'Field Label',
'field_name_2' => 'Field Label'
);

//Now run through the post variables and see if they exist

$content = '';

foreach($_POST as $key=>$val){
if(!empty($val)){
$content .= $arrNames[$key] . ': '.$val .'\r\n';
}
}

//Now do you mail call
mail("$receiver", "$subject!", "$content", "From: $headers");
header("Location: thankyou.html");



I hope thats right. Been a long day.

csparks
06-09-2009, 07:31 PM
Oh and one more thing, you dont need those quotes around the $receiver and $content, and if you eliminate the exclamation point, you dont need it on $subject either. I would also define the full headers in your $headers variables, and then eliminate the quotes around headers.

Sorry I missed that when I copied and pasted your mail send code.

kayz
06-09-2009, 10:12 PM
Really appreciate your help guys, still learning PHP.


I would have done this like this:


//Set the field labels for each form element here
$arrNames = array(
'field_name' = > 'Field Label',
'field_name_2' => 'Field Label'
);

//Now run through the post variables and see if they exist

$content = '';

foreach($_POST as $key=>$val){
if(!empty($val)){
$content .= $arrNames[$key] . ': '.$val .'\r\n';
}
}

//Now do you mail call
mail("$receiver", "$subject!", "$content", "From: $headers");
header("Location: thankyou.html");



I hope thats right. Been a long day.

unexpected '=', expecting ')' (where the field names are)



You can try some thing like this

<?php

$mypostData = array();

foreach($_POST as $key=>$value) {
if($value != "") {
$mypostData[$key] = $value;
}
}

print_r($mypostData); // This will show all the fields that have values inside it


Let me know how it goes please

Hmm this returns the following error:

Array ( [name_on_card] => hfgh [credit_card_number] => fghf )
Warning: Cannot modify header information - headers already sent by (output started at /home/locateme/public_html/test/form_process.php:64) in /home/locateme/public_html/test/form_process.php on line 67


It still remains to display field names when they are carrying no entry when its being sent to my email.

rasin
06-10-2009, 04:34 AM
Any output before header() function will produce such types of error
so make sure that no echo,print,print_r(),var_dump() statemets before header()

in this case remove that print_r() statement

hope it helps you

kayz
06-10-2009, 07:20 AM
I think i am making an error somewhere by placing it in the wrong place? I tried csparks solution by replacing the field names with my session variables right?

With HivelocityDD's solution i placed his just above my headers...


Here are the full test form that im getting to work if it helps somebody.


Index.php


<form method="post" action="form2.php">
Name
<input type="text" name="name">
Email
<input type="text" name="email_address">
<input type="submit" value="Go To Step 2">
</form>


form2.php


<?php

//let's start the session
session_start();

//now, let's register our session variables
session_register('name');
session_register('email_address');

//finally, let's store our posted values in the session variables
$_SESSION['name'] = $_POST['name'];
$_SESSION['email_address'] = $_POST['email_address'];

?>
<form method="post" action="form3.php">
membership_type
<input type="radio" group="membership_type" value="Free">
<input type="radio" group="membership_type" value="Normal">
<input type="radio" group="membership_type" value="Deluxe">
terms_and_conditions
<input type="checkbox" name="terms_and_conditions">
<input type="submit" value="Go To Step 3">
</form>


form3.php


<?php

//let's start the session
session_start();

//now, let's register our session variables
session_register('membership_type');
session_register('terms_and_conditions');

//finally, let's store our posted values in the session variables
$_SESSION['membership_type'] = $_POST['membership_type'];
$_SESSION['terms_and_conditions'] = $_POST['terms_and_conditions'];

?>
<form action="form_process.php" method="post">
name_on_card
<input type="text" name="name_on_card">
credit_card_number
<input type="text" name="credit_card_number">
credit_card_expiration_date
<input type="text" name="credit_card_expiration_date">
<input type="submit" value="Submit">
</form>




form_processes.php


<?php

//let's start our session, so we have access to stored data
session_start();

$receiver = 'PUT IN YOUR EMAIL ADDRESS';
$subject = 'Website - Feedback';
$headers = 'FROM EMAIL ADDRESS';


$err_msg = '';
if((!$name)){
$err_msg = '<br />The following fields are missing, please insure you have filled out each field <br /><br />';

if(!$name){
$err_msg .= "* Your First Name<br />";
}
include 'index.php';

exit();
}


$arr= array();

$arr[0] = "\n\n Name: ";
$arr[1] = $_SESSION['name'];

$arr[2] = "\n\n email_address: ";
$arr[3] = $_SESSION['email_address'];


$arr[4] = "\n\n membership_type: ";
$arr[5] = $_SESSION['membership_type'];


$arr[6] = "\n\n terms_and_conditions: ";
$arr[7] = $_SESSION['terms_and_conditions'];


$arr[8] = "\n\n name_on_card: ";
$arr[9] = $_POST['name_on_card'];



$arr[10] = "\n\n credit_card_number: ";
$arr[11] = $_POST['credit_card_number'];


$arr[12] = "\n\n credit_card_expiration: ";
$arr[13] = $_POST['credit_card_expiration_date'];


$content = ($arr[0] . $arr[1] . $arr[2] . $arr[3] . $arr[4] . $arr[5] . $arr[6] . $arr[7] . $arr[8] . $arr[9] . $arr[10] . $arr[11] . $arr[12] . $arr[13]);


mail("$receiver", "$subject!", "$content", "From: $headers");
header("Location: thankyou.html");

?>


Cheers