php variable question

by 3 replies
4
I'm setting up a contact form where the user chooses the person to email from a dropdown list. I can't seem to get the dropdown value to pass properly, I'm sure I'm missing something and was wondering if anyone can help:

Here's the HTML:
Code:
<div class="form">What is your question related to: 
<select name="posSubject" id="posSubject">
<option value="bill" selected>General Question</option>
<option value="john">Warranty Question</option>
<option value="cory">Estimating</option>
<option value="bob">Service Question</option>
</select>
Here's the code in my mail script:
Code:
$yourEmail = $_POST["posSubject"] . '@mydomain.com';
It never picks up the posSubject value and the output is just "@mydomain.com" The select box is between the form tags, I'm using POST method, and the other fields are working fine. Any ideas?

Thanks!
#programming #php #question #variable
  • Well that should work. There maybe some Javascript interfering elsewhere on the page (the id on the select hints at this). There could be an issue with a tag not closed too.

    I find it helps to isolate the problem, to which end I put your code in a test page (see below) and this verifies it should work:-

    <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
    What is your question related to:
    <select name="posSubject" id="posSubject">
    <option value="bill" selected>General Question</option>
    <option value="john">Warranty Question</option>
    <option value="cory">Estimating</option>
    <option value="bob">Service Question</option>
    </select>
    <button type="submit">Submit</button>
    </form>

    <?php
    if(isset($_POST['posSubject'])) {
    $yourEmail = $_POST["posSubject"] . '@mydomain.com';
    echo "<br /><br /><br />You chose $yourEmail";
    }
    ?>


    Run the above code and it produces what you were expecting.
  • It will work only if you are pointing back to the same page. You have to refresh the page to be able to do the $POST. Hence the <form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">

    Without the page refresh, you're value will not work at all. Posts and Gets can be tricky.

    Frank
  • A couple of tips, make sure you are ending any other form tags that could be above this form, there have been one or two times I have been rushing and this caused 10 minutes of debugging headaches another could be that you are calling your div "form" not sure if that is a reserved word, I always stay away from mixing names (mysql issues taught me that ).

    Otherwise everything that you posted looks OK. If you want to email me the whole form page I can take a look, just let me know and I will PM an email address

Next Topics on Trending Feed

  • 4

    I'm setting up a contact form where the user chooses the person to email from a dropdown list. I can't seem to get the dropdown value to pass properly, I'm sure I'm missing something and was wondering if anyone can help: Here's the HTML: