php variable question

by coog
3 replies
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!
#php #question #variable
  • Profile picture of the author Martyn Walker
    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.
    {{ DiscussionBoard.errors[1709640].message }}
  • Profile picture of the author fthomas137
    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
    {{ DiscussionBoard.errors[1712540].message }}
  • Profile picture of the author boo5td
    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
    {{ DiscussionBoard.errors[1713073].message }}

Trending Topics