Page 1 of 1

PHP Help Needed

Posted: Tue Mar 30, 2010 2:14 pm
by p.persia69
Hi,
I need help in my PHP code, this simply takes all values from all text boxes on my form and sends all to my email.
The problem is that it reverses the fields, for example, 1st field is name, 2nd is email and 3rd is message on my form, but this form sends it to me in reversed order!

This is the first lines of my code which collects all fields and I think should be changed:
Code:

Code: Select all

<?php
// Create Message Text
$form_fields=array_keys($HTTP_POST_VARS);
while($field=array_pop($form_fields)){
  if ($field != "To")
    $temp.=" $field : = $HTTP_POST_VARS[$field] \n";
}
// Read POST request params into global vars
$from    = $_POST['Email'];
$message = $temp;
// Obtain file upload vars
$fileatt      = $_FILES['File']['tmp_name'];
$fileatt_type = $_FILES['File']['type'];
$fileatt_name = $_FILES['File']['name'];
...
Please anyone knows how can I do it? I am newbie at PHP.
Thanks all :x

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 2:38 pm
by AbraCadaver
Well, array_pop() pops the last element off of the array and then you add that to $temp, essentially reversing them. Lot's of ways to do this, but to keep inline with your code, try this:

Code: Select all

// Create Message Text
$temp = '';
foreach($_POST as $key => $value) {
    if($key != 'To') {
        $temp .= "$key : = $value\n";
    }
}
Although, the easiest fix would be to change array_pop() to array_shift() :-) But the original code looks wacky and you shouldn't use $HTTP_POST_VARS as it is deprecated and may not be enabled on current systems anyway.

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 3:00 pm
by p.persia69
Hi buddy
Thank you for your help :X

Code: Select all

<?php

...

// Create Message Text

$form_fields=array_keys($HTTP_POST_VARS);
$temp=" Sender's IP Address : = " . $_SERVER['REMOTE_ADDR'] . " - " . $countryName . "\n";
while($field=array_pop($form_fields)){
  if ($field != "To")
    $temp.=" $field : = $HTTP_POST_VARS[$field] \n";
}

$temp1 = '';
foreach($_POST as $key => $value) {
  if($key != 'To') {
    $temp1. = "$key : = $value \n";
  }
}
...
Getting parse error, where I am wrong? :(

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 3:18 pm
by AbraCadaver
First, my loop was a replacement for the one you had. Secondly, your spaces are messed up. You need this:

Code: Select all

$temp1 .= "$key : = $value \n";

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 4:02 pm
by p.persia69
Thank you man, it's OK now :D

Just one more question :)
It will send all fields now, including a Captcha and Submit ?!
How can I remove these 2 fields?
This is a sample email I receive now:

Sender's IP Address : = 38.98.x.x - United States
FullName : = gdsf
Serial : =
Email : = fsd@dsfa.das
Topic : = Sales Query
Description : = Message
Captcha : = LFNF
Submit : = Submit

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 4:19 pm
by jbulaswad
Swap this statement

Code: Select all

if ($field != "To")
with this statement

Code: Select all

if(!in_array($field, array("To", "Captcha", "Submit")))

Re: PHP Help Needed

Posted: Tue Mar 30, 2010 4:30 pm
by AbraCadaver
Yes. But really you should be collecting known values and not collecting all values except certain ones. As an example, instead of the loop, I would use:

Code: Select all

$temp = '';
$temp .= 'FullName = ' . $_POST['FullName'] . "\n";
$temp .= 'Serial = ' . $_POST['Serial'] . "\n";
$temp .= 'Email = ' .  $_POST['Email'] . "\n";
$temp .= 'Topic = ' . $_POST['Topic'] . "\n";
$temp .= 'Description = ' . $_POST['Description'] . "\n";
You should also be checking for spam/malicious content. Earlier I was just attempting to explain your initial problem and show you a possible alternative.