LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   processing forms with PHP (https://www.linuxquestions.org/questions/programming-9/processing-forms-with-php-698957/)

3vra 01-21-2009 03:31 PM

processing forms with PHP
 
I am trying to process an email form using PHP.It is working okay so far but I want the Items to be displayed as a list when the email is received.
eg.

one
two
three
four

and not

one two three four

here is a copy of the variable that displays the message

TB0ne 01-21-2009 03:57 PM

Quote:

Originally Posted by 3vra (Post 3416594)
I am trying to process an email form using PHP.It is working okay so far but I want the Items to be displayed as a list when the email is received.
eg.

one
two
three
four

and not

one two three four

here is a copy of the variable that displays the message

Don't have the variable...you didn't attach it. Check out the PHP manual at http://us.php.net/explode, specifically, the explode command.

3vra 01-22-2009 08:23 AM

Variable I left out (item)
 
foreach ($_POST['MLIS'] as $item){
$items .= $item." ";

}

here is the copy of the variable I left out

jiml8 01-22-2009 10:30 AM

Quote:

Originally Posted by 3vra (Post 3417350)
foreach ($_POST['MLIS'] as $item){
$items .= $item." ";

}

here is the copy of the variable I left out

$items .= $item."\n";

3vra 01-23-2009 02:20 PM

could someone please explain to me why this codes sends only the information checked in the check boxes and not the text boxes.


<tr>
<td><input type="checkbox" name="MLIS[]" value="Notepads" />Notepads</td>
<td><input type="text" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }" ></td>
</tr>

this is the code used for sending the items to the email but only the items in the check boxes show up

foreach ($_POST['MLIS'] as $item){
$items .= $item ."\n";


the items are stored in the php variable $items

michaelk 01-23-2009 03:15 PM

You really have not provided enough of your code. However, from what you did post the text box is not stored in the MLIS array.

3vra 01-23-2009 04:32 PM

Quote:

Originally Posted by michaelk (Post 3419001)
You really have not provided enough of your code. However, from what you did post the text box is not stored in the MLIS array.


<form name"MUD LAB INVENTORY SYSTEM" method="post" action="index.php">
<tr>
<td><input type="checkbox" name="MLIS[]" value="Notepads" />Notepads</td>
<td><input type="text" name="quantity_1" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }" ></td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Envelopes" />Envelopes</td>
<td> <input type="text" name="quantity_2" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"></td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Staples" />Staples</td>
<td><input type="text" name="quantity_3" size="1" maxlength="2"onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"></td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Post It Notes" />Post It Notes</td>
<td><input type="text" name="quantity_4" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"></td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Red Ink Pens" />Red Ink Pens</td>
<td><input type="text" name="quantity_5" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"> </td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Black Ink Pens" />Black Ink Pens</td>
<td> <input type="text" name="quantity_6"size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"> </td>
</tr>
<tr>
<td><input type="checkbox" name="MLIS[]" value="Red Ink Pens" />Blue Ink Pens</td>
<td><input type="text" name="quantity_7" size="1" maxlength="2" onkeydown="val = this.value;" onkeyup="if (/\D/.test(this.value)) { this.value = val; }"></td>
</tr>
<tr>
<td><input type="submit" value= "Send"></td>
</tr>
</form>
</table>
<?php
}
else{
error_reporting(0);
$email="somebody@somewhere.com"
$subject="Subject";
$body= "Dear Joyce,\n\nBelow is a list of all the items that are currently needed at the Mud Lab:";
$items="";
$footer = "Regards"." "."staff_member";
$staff_member = "Staff Member";
foreach ($_POST['MLIS'] as $item){
$items .= $item ."\n";
}
$msg_body = $body."\n\n$items.\n\n".$footer;
$recipient = "somebody@somewhere.com";
$from = stripslashes($email);
$subject = stripslashes("Test Mail");
$msg = "Message from: $from\n\n".stripslashes($msg_body);
$header = 'From: "somebody@somewhere.com"'."\r\n".'Reply-To: $staff_member'."\r\n". 'X-Mailer: PHP/' . phpversion();
if (mail($recipient, $subject, $msg, $header)){
echo ("<b>Message Sent To:</b> $recipient");
} else
echo "Message failed";
}

michaelk 01-23-2009 08:44 PM

Homework?

3vra 01-26-2009 08:56 AM

Quote:

Originally Posted by michaelk (Post 3419243)
Homework?

No.I'm just learning HTML and PHP and trying things out.I just need an explanation on what's going on so I can understand why I am not getting the results I want.

michaelk 01-26-2009 10:33 AM

Your PHP code is only processing the MLIS array which only contains check box values. You need to get the data from the text boxes. It is possible to use an array for the text boxes but the lengths will be different i.e. MLIS vs textbox.

3vra 01-26-2009 11:08 AM

Quote:

Originally Posted by michaelk (Post 3421732)
Your PHP code is only processing the MLIS array which only contains check box values. You need to get the data from the text boxes. It is possible to use an array for the text boxes but the lengths will be different i.e. MLIS vs textbox.

\

now I get it.I used an array for the text boxes used another foreach statement with the required variables for the text boxes and it worked.Thanks man

3vra 02-12-2009 12:52 PM

the logic seems right but it does not work
 
I am trying to write a block of code in php that will echo an error if the value entered in a text box is not an integer.The logic behind the code seems correct but when the form is processed it returns the erro message even if the value entered is an int.

SAMPLE CODE
===========
if (($_POST['quantityBox'.$i] != "") && (! is_int ($_POST['quantityBox'.$i]))) {
$errorMessage[] = "only numbers are allowed in the quantity box!";
}

frieza 02-12-2009 01:41 PM

first of all it might be better to replace
Code:

($_POST['quantityBox'.$i] != "")
with
Code:

(isset(($_POST['quantityBox'.$i]))
second, form input always returns as a string, try

is_numeric() instead of is_int()

3vra 02-12-2009 01:50 PM

Quote:

Originally Posted by frieza (Post 3441348)
first of all it might be better to replace
Code:

($_POST['quantityBox'.$i] != "")
with
Code:

(isset(($_POST['quantityBox'.$i]))
second, form input always returns as a string, try

is_numeric() instead of is_int()


is_numberic () was the correct funtion to use. THANKS!

frieza 02-12-2009 02:24 PM

yw ;)
p.s. the first one i mentioned the ($_POST['quantityBox'.$i] != "") vs (isset(($_POST['quantityBox'.$i])) at first blush may not seem to be any different but especially when dealing with checkboxes the variable is only null until its been used once then it becomes an array of nulls if nothing is selected, isset() should be able to deal with that but if !="" will not.


All times are GMT -5. The time now is 01:05 PM.