Jump to content

Array problem


Guest osonegro

Recommended Posts

Guest osonegro

Good day,

 

I am new at this fascinated PHP program. I am very interesting to learn it by myself, I've been reading books in the book store and thru the internet. (Pardon me I must put the books back, cuz I don't know if they are helpful or not)

 

So, here is my case:

 

In a table of a form I have this:

 

<td valign=top bgcolor=006699 width="293"><font color=ffffff><b>First Name</b></font></td>

<td width=406 bgcolor=eeeeee>

<input name=first_name id=first_name type=text style="font-size:10pt; width:400px"/>

</td>

</tr>

<tr>

<td valign=top bgcolor=006699 width="293"><font color=ffffff><b>Last Name</b></font></td>

<td width=406 bgcolor=eeeeee>

<input name=last_name id=last_name type=text style="font-size:10pt; width:400px"/>

</td>

<td bgcolor=eeeeee width="9"><font color=red>*</font></td>

</tr>

 

and also, I have those check boxes

 

<input type="checkbox" name="products[]" value="Product 1"/>

<label for="Product 1">Product 1</label><br>

 

<input type="checkbox" name="products[]" value="Product 2"/>

<label for="Product 2">Product 2</label><br>

 

<input type="checkbox" name="products[]" value="Product 3"/>

<label for="Product 3">Product 3</label><br>

 

 

In a php file I have this:

 

<?php

$to = "username@webhost.com";

$from = $_REQUEST['email'];

$name = $_REQUEST['first_name'];

$headers = "From: $from";

$subject = "New product";

 

$fields = array();

 

$fields{"first_name"} = "First Name";

 

$fields{"last_name"} = "Last Name";

 

$fields{"products"} = "Products and Services";

 

$body = "New product:\n";

foreach($fields as $a => $b )

{

$body .= sprintf("%s: %s\n\n",$b,$_REQUEST[$a]);

}

$headers2 = "From: username@webhost.com";

$subject2 = "Thank you for register at webhost";

$autoreply = "\nThank you for register at webhost. We will get back to you as soon as possible.\n";

$send = mail($to, $subject, $body, $headers);

$send2 = mail($from, $subject2, $autoreply, $headers2);

if($sent)

{print "Thank you for your registration";}

else

{print "We didn't get your info. Please try again.";}

?>

 

In the result:

 

First Name: Good

 

Last Name: Good

 

Products and Services: Array (There is something wrong with the array)

 

Could anyone give me a tip?

Link to comment
Share on other sites

I assume the product array only gives you something like [0 0 1] depending on if they were selected or not right? ( Can't remember what the set for them are)

 

Anyway radio buttons have multiple choices, but one and only one value is transmitted for each group. If no radio button of a group is checked, the action of the client browser is unpredictible. For that reason, you should always specify the checked attribute for one button of a group, assuring that a button will be checked when the form is transmitted.

 

Also you are using the super global array $_REQUEST = $_POST or $_GET .. now in order to access the info in there you have to actually send info using a form. And I suspect you might have an issue with this.

 

 

Also this looks strange to me, where do you have the info sent from the form with the checkboxes?

Especially the foreach loop looks very suspicious.

 

$fields = array();

$fields{"first_name"} = "First Name";

$fields{"last_name"} = "Last Name";

$fields{"products"} = "Products and Services";

$body = "New product:\n";
foreach($fields as $a => $b )
{
{
$body .= sprintf("%s: %s\n\n",$b,$_REQUEST[$a]);
}

 

I mean you are looping the $fields array in that loop and trying to access $_REQUEST[[first_name ... and so on]) when in fact what you got is a miltidimensional array let say $_REQUEST[products[]]

 

where products[] is an array holding the info of which boxes were checked. You have to relook the logic in your script.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...