0

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

5
  • Are they included in the same file? Commented Jan 25, 2021 at 17:24
  • 2
    The productlist() function has to return something if you want to call is like $message = productlist($itemname, $itemquantity); and NOT echo anything Commented Jan 25, 2021 at 17:25
  • return an array to implode or a concatenated/built string or use ob_start/ob_get_clean, lots of ways but simply echoing is not correct Commented Jan 25, 2021 at 17:26
  • The question could have better been posted at codereview.stackexchange.com Commented Jan 25, 2021 at 17:56
  • Sounds like you need implode and an array. Commented Jan 25, 2021 at 17:57

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.