PHP Help Needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
p.persia69
Forum Newbie
Posts: 3
Joined: Tue Mar 30, 2010 2:10 pm

PHP Help Needed

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Help Needed

Post 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.
Last edited by AbraCadaver on Tue Mar 30, 2010 2:50 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
p.persia69
Forum Newbie
Posts: 3
Joined: Tue Mar 30, 2010 2:10 pm

Re: PHP Help Needed

Post 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? :(
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Help Needed

Post 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";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
p.persia69
Forum Newbie
Posts: 3
Joined: Tue Mar 30, 2010 2:10 pm

Re: PHP Help Needed

Post 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
jbulaswad
Forum Newbie
Posts: 14
Joined: Tue Mar 30, 2010 2:37 pm
Location: Detroit, Michigan, USA

Re: PHP Help Needed

Post 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")))
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: PHP Help Needed

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply