简体   繁体   中英

Passing dynamic data from JavaScript to PHP

I have somewhat of an "order" page set up that allows people to select items for purchase. When they hit "order" it takes them to a page that allows them to select details for each item drom dynamically populated drop downs. These drop downs are in a loop so that each item has its own set.

This is where my issue starts. If there is only one item on the order page it works perfectly. All the selected info from the drop downs is pulled into my PHP script, and it formats it into an email and sends the order to me. If there is more than 1 item in the order, it will only send the last item on the page.

Here is the code for the dropdowns...

<form name="mediumselect">
<table>
<tr>
    <td> 
        <select name="sel" onchange="selFormat(this);" style="width:200px">
        <option value="">-Please select delivery format-</option>
        <option value="File">File</option>
        <option value="Tape">Tape</option>
        <option value="Disc">Disc</option>
        </select>
    </td>
    <td>
        <select name="formats" onchange="selRate(this)" style="width:150px">
        </select>
    </td>
    <td>
        <select name="framerate" onchange="selColor(this)" style="width:150px">
        </select>
    </td> 
    <td>
        <select name="color" style="width:150px">
        </select>
    </td>
</table>
</form>

Here is the PHP...

<?php

$asset = $_REQUEST['assetname'];
$medium = $_REQUEST['sel'];
$type = $_REQUEST['formats'] ;
$framerate = $_REQUEST['framerate'] ;
$color = $_REQUEST['color'] ;
$body = <<<EOD

Asset Name: $asset
Asset format: $medium
Format Type: $type
Frame Rate/Resolution: $framerate
Codec or Color Subsample: $color
EOD;

$results = <<<EOD
<html>
<head>
<title>sent message</title>
<style type="text/css">
</style>
</head>
<div align="center">One your order is submitted, a confirmation email will be sent to above email with order details.</div>
</div>
</body>
</html>
EOD;
function spamcheck($field)
{
    //filter_var() sanitizes the e-mail
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //filter_var() validates the e-mail
    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

if (isset($_REQUEST['email']))
{
    //if "email" is filled out, proceed
    //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['email']);
    if ($mailcheck==FALSE)
    {
        echo "Invalid input";
    } else  {
        //send email
        global $body;
        global $results;
        $email = $_REQUEST['email'] ;
        mail("~~~@gmail.com", "Subject: INCOMING ORDER", $body, "From: $email" );
        echo "$results";
    }
} else {//if "email" is not filled out, display the form
    echo "<form method='post' action='mailform.php'>
    Email: <input name='email' type='text'><br>
    Subject: <input name='subject' type='text'><br>
    Message:<br>
    <textarea name='message' rows='15' cols='40'>
    </textarea><br>
    <input type='submit'>
    </form>";
}
?>

I am still new to PHP and if there is a better way to go about doing this I would be grateful for the advice.

I would recommend putting all the $body values into a single string. So first loop through every item and just add to the string $body using PHP concatenation ($body . =). Currently you are only retrieving one value. Perhaps change the names of your fields by looping through them for how many items there are ++ a number following the name in order to loop through those _REQUESTS for that those fields in put them into you $body .= string for email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM