SamSam Kwong
SamSam Kwong

Reputation: 1

PHP function with array

Just got the coding question about php

==============html input form=========================
<input name="itemname[]" type="text" readonly="readonly" value="iphone">
<input name="itemquantity[]" type="text" value="3">
 
<input name="itemname[]" type="text" readonly="readonly" value="samsung">
<input name="itemquantity[]" type="text" value="6">
<input name="itemname[]" type="text" readonly="readonly" value="IBM">
<input name="itemquantity[]" type="text" value="3">

==============PHP code(part1)==============
function productlist($itemname,$itemquantity) {
    for($i=0;$i<count($itemname);$i++)
    {
        echo $itemname[$i] .",". $itemquantity[$i]."<br>";
    }
}
$message = productlist($itemname, $itemquantity);

==============PHP code(part2)==============
$to = "[email protected]";
$subject = "Online order form";
mail($to, $subject, $message, $headers);

part1 result:
iphone,3
samsung,6
IBM,3

Question: the coding can run with part 1 result, may i know how to pass part1 result into part2 and can send out successfully.

thx so much

Upvotes: 0

Views: 45

Answers (1)

Jason K
Jason K

Reputation: 1381

Readonly is a Bool value so you don't have to set it to anything. Not that you should trust data from the client. It can be easily over written.

Your server will have to be configured to send email. If it's not that's a different problem to work on.

==============html input form=========================
<input name="itemname[]" type="text" readonly value="iphone">
<input name="itemquantity[]" type="text" value="3">
 
<input name="itemname[]" type="text" readonly value="samsung">
<input name="itemquantity[]" type="text" value="6">
<input name="itemname[]" type="text" readonly value="IBM">
<input name="itemquantity[]" type="text" value="3">

==============PHP code(part1)==============
$message = productlist($_POST['itemname'], $_POST['itemquantity']);

function productlist($itemname,$itemquantity) {
  $message = '';
  for($i=0;$i<count($itemname);$i++){
    $message .= $itemname[$i] .",". $itemquantity[$i]."<br>";
  }
  return $message;
}

==============PHP code(part2)==============
$to = "[email protected]";
$subject = "Online order form";
mail($to, $subject, $message, $headers);

Upvotes: 1

Related Questions