Jump to content

$Post Multiple Variable from a Single <option>


jperez260

Recommended Posts

Hello,

 

I'm running php using classes and here's my dilema.  I have a web form that pull data from an MySQL database as a <SELECT> <OPTION> drop down as a while loop; I have two while loops and no problem getting data for each.  I have no problem calling the data for the <OPTION> however I need to pull two pieces of information and the <OPTION> from what I know only pulls one. The second while loop from the "sender" table has a "sender_name" & "sender_email".  My problem is I can only have one value in the <OPTION> tag but I need to $POST two variables. when I select my drop down which only displays "sender_name" I would like to post "sender_email" so that I can echo this in my functions page for other things.

 

My MySQL table for "sender" has the following: sender_id, sender_name, sender_email

 

Plan is to create an Entry Form > Form Submitted > Data is run and verified on functions page > if Good execute code and send email to sender with details of the entry data...

 

Currently I have it working if I enter a static email address in the mail() function however I would like when the Entry Form is filled out for it to pull the "sender_email"  from the associated "sender_name" on that table as a variable to the functions page which has a mail() script

 

Should I be using JavaScript or another PHP function?

 

Thank you in advance, my code below

 

My form:

<form method="post" action="form-post.php">
                <input type="hidden" name="add-tracking" value="true" />
                <input type="hidden" name="tracking_method_id" id="tracking_method_id" value="2" />
                <table>
                <tr>
                    <td><label for="tracking_number">Tracking Number:<br />(No Dashes)</label></td>                   
                    <td><input type="text" name="tracking_number" id="tracking_number" /></td>
                </tr>
                
                <tr>
                    <td><label for="tracking_recepient">Tracking Recepient:</label></td>
                    <td><input type="text" name="tracking_recepient" id="tracking_recepient" /></td>
                </tr>
                
                <tr>
                    <td><label for="tracking_date">Tracking Date:<br />(YYYY-MM-DD)</label></td>
                    <td><input type="text" name="tracking_date" id="tracking_date" value="<?php echo date('Y-m-d');?>" /></td>
                </tr>
                
                <tr>
                    <td><label for="tracking_cost">Cost $:</label></td>
                    <td><input type="text" name="tracking_cost" id="tracking_cost" value="0.0000" /></td>
                </tr>
                
                <tr>
                    <td><label for="tracking_dept">Dept:</label></td>
                    <td><select name="tracking_dept" id="tracking_dept">
                        <option value=""></option>
                        <?php
                        $sql = "SELECT * FROM department";
                        $res = mysql_query($sql) or die(mysql_error());
                        while($row = mysql_fetch_assoc($res)) {
                        echo '<option value="' . $row['dept_name'] . '">' . $row['dept_name'] . '</option>';}
                        ?>   
                    </select></td>
                </tr>
                
                <tr>
                    <td><label for="tracking_requestor">Requested By:</label></td>
                    <td><select name="tracking_requestor"  id="tracking_requestor">
                        <option value=""></option>
                        <?php
                        $sql1 = "SELECT * FROM sender";
                        $res1 = mysql_query($sql1) or die(mysql_error());
                        while($row1 = mysql_fetch_assoc($res1)) {
                            echo '<option value="' . $row1['sender_name'] . '" $row1>' . $row1['sender_name'] . '</option>';
                          }
                        ?>
                    </select></td>
                </tr>
                                
                <tr>
                    <td colspan="2"><input type="submit" name="submit" value="Submit" /></td>
                </tr>
                </table>
                </form>

My Function

function add_tracking($p) {
        $tracking_id = mysql_real_escape_string($p['tracking_id']);
        $tracking_number = mysql_real_escape_string($p['tracking_number']);
        $tracking_recepient = mysql_real_escape_string($p['tracking_recepient']);
        $tracking_date = mysql_real_escape_string($p['tracking_date']);
        $tracking_cost = mysql_real_escape_string($p['tracking_cost']);
        $tracking_dept = mysql_real_escape_string($p['tracking_dept']);
        $tracking_requestor = mysql_real_escape_string($p['tracking_requestor']);
        $tracking_method_id = mysql_real_escape_string($p['tracking_method_id']);
        $tracking_number = preg_replace('/[^a-zA-Z0-9s]/', '', $tracking_number);
        $headers = "";
        $headers .= "From: Anaheim Receiptionist <anaheimreceptionist@powerplus.com>\n";
        $headers .= "X-Sender: anaheimreceptionist@powerplus.com\n";
        $headers .= "X-Mailer: PHP\n";
        $headers .= "X-Priority: 3\n";
        $headers .= "Content-Type:text/html; charset=\"iso-8859-1\"\n";
        $message = '
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <style type="text/css">
        .right {text-align: right; min-width: 150px;}
        * {font-family: arial, tahoma, "Times New Roman";}
        body {font-size: 15px;}
        #wrap {width:550px; margin: 0 auto;}
    </style>
    </head>
    <body>
    
    <div id="wrap">
    
    <p><h1>Your Shipping Item Is Scheduled For Delivery</h1></p>
    
    <p><h3>Below Is Your Details:</h3></p>
    
    <div style="border: 1px solid #000000;">
    
    <table cellpadding="5">
        
        <tr>
            <td class="right">Date: </td><td>' . $tracking_date . '</td>
        </tr>
        
        <tr>
            <td class="right">Sent To: </td><td>' . $tracking_recepient . '</td>
        </tr>
    
        <tr>
            <td class="right">Tracking Number: </td><td><a href="' . $row1['method_link'] . $tracking_number . '">' . $tracking_number . '</a></td>
        </tr>
    
        <tr>
            <td class="right">Department No: </td><td>' . $tracking_dept . '</td>
        </tr>

        <tr>
            <td class="right">Sent By: </td><td>' . $tracking_requestor . '</td>
        </tr>
    
    </table>
    
    </div>
    
    </div>
    
    </body>
    </html>
    ';
        
        if(!$tracking_number || !$tracking_recepient || !$tracking_date || !$tracking_cost || !$tracking_dept || !$tracking_requestor):
        
            if(!$tracking_number):
                echo '<p>The Tracking Number is required</p>';
            endif;
            if(!$tracking_recepient):
                echo '<p>The Recepient Name is Required</p>';
            endif;
            if(!$tracking_date):
                echo '<p>The Tracking Date is Required</p>';
            endif;
            if(!$tracking_cost):
                echo '<p>The Tracking Amount is Required</p>';
            endif;
            if(!$tracking_dept):
                echo '<p>The Tracking Dept is Required</p>';
            endif;
            if(!$tracking_requestor):
                echo '<p>The Requested By Name is Required</p>';
            endif;
        
            echo '<p><a href="tracking-method.php">Try again!</a></p>';
        else:
            $sql = "INSERT INTO tracking VALUES (null, '$tracking_number', '$tracking_recepient', '$tracking_date', '$tracking_cost', '$tracking_dept', '$tracking_requestor', '$tracking_method_id')";
            $res = mysql_query($sql) or die(mysql_error());
            echo '<p>Added Sucessfully</p>';
            echo '<p><img src="ok.png" />';
            echo '<p><a href="index.php">Go Back to Tracking # Table</a></p>';

            mail($sender_email, 'Your Tracking ID: ' . $tracking_number, $message, $headers);

        endif;
    }
Link to comment
Share on other sites

Easiest way is to simply run a query during the validation process to grab the email based on the posted name.  Second is to use a hidden field to hold the email and use jquery to send an ajax request when a name is selected and slip the email in the hidden field dynamically.  Last is to use the new data fields in the attribute tags of the option tag and place the email there and then again use jquery to grab that value and put it in the hidden field when the name is selected. 

Link to comment
Share on other sites

A somewhat harder way, that is, if you want all the desired data to come in on the form submit, is to concatenate everything into one string:

 

$sender_name . "|" . $sender_email

 

to populate the HTML, then

 

explode("|", $_POST)

Edited by bsmither
Link to comment
Share on other sites

Update: I got the email to populate in the mail() function by creating a. SQL statement and making the query unique. My new problem now is that in the $message it is grabbing the variables due them being posted from the form but I'm trying call dbase columns to grab a few extra field for the $message template. I suppose due to them not being called from the post form it's not calling the data. I even did another SQL statement but not working. Very frustrating :-/. Let me know if I need to elaborate further. I really appreciate your time. Thank you

Link to comment
Share on other sites

UPDATE 2: 

Well Finally got this to work.  I needed to add some hidden input fields so they would "form post" to my functions page then I was able to call the variable and display on my auto email. Your suggestions are very interesting; I will keep these in mind as alternative methods.  Thank you again

Edited by jperez260
Link to comment
Share on other sites

You're doing this the wrong way. The only thing you should be posting is the ID of the selected value. The SELECT field on your form page should use the sender name as the displayed text for the options and the sender ID as the Value. Then on the receiving page you would use the passed ID to run a query to get the name and email address to use in the process of sending the email. What you are doing now would let a malicious user re-purpose your form to send SPAM.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.