PDA

View Full Version : Get value of selected in dropdown list



Bicklo
06-12-2017, 05:40 PM
I am trying to post the value of the selected in a dropdown list but no luck, can anyone help me please.



<div class="mailvanform">

<select id="owner">
<?php
$sql = mysql_query( "SELECT email FROM namen");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['email'] . "</option>";
}

echo = $_POST['owner1'];


?>
</select>


</div>

jscheuer1
06-12-2017, 06:26 PM
Is it a form? You cannot post without at least one of either:

A.) A form submission under the method POST.

Or:

B.) An AJAX call via javascript, also under the method POST.

I don't see either in your code snippet.

A further potential issue I think see is that the value of all the options appears to be the same ("owner1").

Now, I'm not even sure if you understand what $_POST is. It's an array of values that were posted to the PHP page/script. It will be an empty array if nothing has been posted. You cannot post to a page that's already loaded. And, unless you use javascript, you cannot get a value from a form or form element without submitting the form (which will reload the page or load some other resource). And, if using javascript alone to get the value, that value will be temporary unless it's sent to the server using AJAX in some fashion. Though you could get the value via javascript, and later the user could submit the form. If you know all that and just haven't shown those portions of the code, please include or explain it in detail it next time.

Bicklo
06-12-2017, 08:16 PM
Thank you for your reply,

It's a drop down list with items from a database, I want to send mail via php and it want work.

So I try to find a way to achieve this.





<body>

<div class="water">

<form>




<!-- SELECT SENDER -->


<div class="mailtop">




<div class="mailvan"><h1 class="hmailvan">Van:</h1></div>

<div class="mailvanform">

<select name="owner">
<?php
$sql = mysql_query( "SELECT email FROM namen");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['email'] . "</option>";
}
?>
</select>


</div>

</div>



<!-- END SELECT SENDER -->









<!-- SELECT MAIL RECIPIENT -->



<div class="mailvoor"><div class="mailvoortitel"><h1 class="hmailvan">Aan:</h1></div>

<div class="mailvoorform">

<?php
if(isset($_POST['lombi'])){

$mailvoor = $_POST['lombi'];

}

if(isset($_POST['variable'])){

$mailonderwerp = $_POST['variable'];




$eind1= mysql_query("SELECT e1 FROM items WHERE item='$mailonderwerp'");

while ($row = mysql_fetch_object($eind1)){

$titel1=$row->e1;


};

$mails1= mysql_query("SELECT email FROM namen WHERE titel='$titel1'");

while ($row = mysql_fetch_object($mails1)){

echo $row->email.' ; ';


};



$eind2= mysql_query("SELECT e2 FROM items WHERE item='$mailonderwerp'");

while ($row = mysql_fetch_object($eind2)){

$titel2=$row->e2;


};

$mails2= mysql_query("SELECT email FROM namen WHERE titel='$titel2'");

while ($row = mysql_fetch_object($mails2)){

echo $row->email.' ; ';


};




}



$vtest = mysql_query("SELECT d1, d2, d3 FROM items WHERE item='$mailonderwerp'");




while ($row = mysql_fetch_array($vtest)){
$doen1 = $row['d1'];
$doen2 = $row['d2'];
$doen3 = $row['d3'];


}


if ($doen1 !=NULL){
$xtest = mysql_query("SELECT email FROM namen Where titel ='$doen1''");




while ($row = mysql_fetch_object($xtest)){
echo '<p><a href=""><div class="namen">'.$row->email.'</div></a></p>';



}
}





if ($doen2 !=NULL){


$ytest = mysql_query("SELECT * FROM $doen2");




while ($row = mysql_fetch_object($ytest)){
echo '<p><a href=""><div class="namen">'.$row->naam.'</div></a></p>';


}
}




if ($doen3 !=NULL){


$ztest = mysql_query("SELECT * FROM $doen3");




while ($row = mysql_fetch_object($ztest)){
echo '<p><a href=""><div class="namen">'.$row->naam.'</div></a></p>';


}

}


?>




</div>




</div>


<!-- END SELECT MAIL RECIPIENT -->








<!-- MAIL SUBJECT -->


<div class="mailtitel">



<div class="mailtiteltitel"><h1 class="hmailvan">Onderwerp:</h1></div>


<div class="mailtitelform">

<?php
if(isset($_POST['variable'])){
echo 'Edri '.$_POST['variable'];
}

?>
</div>


</div>


<!-- END MAIL SUBJECT -->






<!-- MAIL TITLE -->


<div class="mailbericht">

<div class="mailberichttitel"><h1 class="hmailvan">Bericht:</h1></div>

<div class="mailberichttitelform">

<textarea class="comment" name="comments"></textarea>


</div>

</div>



<!-- END MAIL TITLE -->



<div class="mailsend">

<div class="poper"><input type="submit" value="versturen"></div>

</div>


</form>



</div>



</body>

jscheuer1
06-13-2017, 03:08 AM
OK, hmm. There're no action or method attributes to the form. The action attribute will tell it what PHP script to or page to goto/run when it's submitted, and the method attribute will tell it how to send (GET or POST) the form data to that PHP page/script. I see no javascript, so I'm guessing that's not part of what you want to do. The page can submit to itself, this is often done, and then based upon whether or not certain POST or GET data is present it can do different things. Still, the most common practice is to submit to a separate script or page.

I hope that helps you get on track. I will have a closer look at your code and this thread though to see if I can figure out what you're intending to do, and if possible give more concrete suggestions.

Bicklo
06-14-2017, 08:39 PM
So far I have this,




<body>

<div class="water">


<form action="mail_handler.php" method="post">

<div class="mailtop">





<div class="mailvan"><h1 class="hmailvan">Van:</h1></div>

<div class="mailvanform">

<select name="owner">
<?php
$sql = mysql_query( "SELECT email FROM namen");
while ($row = mysql_fetch_array($sql)){
echo "<option value=\"owner1\">" . $row['email'] . "</option>";

$van = "<option value=\"owner1\">" . $row['email'] . "</option>"; // here the problem, get the email address from a drop down list

$lopen = $_POST['$van'];

}
?>
</select>


</div>

</div>



<div class="mailvoor"><div class="mailvoortitel"><h1 class="hmailvan">Aan:</h1></div>

<div class="mailvoorform">

<?php
if(isset($_POST['lombi'])){

$mailvoor = $_POST['lombi'];

}

if(isset($_POST['variable'])){

$mailonderwerp = $_POST['variable'];




$eind1= mysql_query("SELECT e1 FROM items WHERE item='$mailonderwerp'");

while ($row = mysql_fetch_object($eind1)){

$titel1=$row->e1;


};

$mails1= mysql_query("SELECT email FROM namen WHERE titel='$titel1'");

while ($row = mysql_fetch_object($mails1)){

echo $row->email.' ; ';


};



$eind2= mysql_query("SELECT e2 FROM items WHERE item='$mailonderwerp'");

while ($row = mysql_fetch_object($eind2)){

$titel2=$row->e2;


};

$mails2= mysql_query("SELECT email FROM namen WHERE titel='$titel2'");

while ($row = mysql_fetch_object($mails2)){

echo $row->email.' ; ';


};




}



$vtest = mysql_query("SELECT d1, d2, d3 FROM items WHERE item='$mailonderwerp'");




while ($row = mysql_fetch_array($vtest)){
$doen1 = $row['d1'];
$doen2 = $row['d2'];
$doen3 = $row['d3'];


}


if ($doen1 !=NULL){
$xtest = mysql_query("SELECT email FROM namen Where titel ='$doen1''");




while ($row = mysql_fetch_object($xtest)){
echo '<p><a href=""><div class="namen">'.$row->email.'</div></a></p>';



}
}





if ($doen2 !=NULL){


$ytest = mysql_query("SELECT * FROM $doen2");




while ($row = mysql_fetch_object($ytest)){
echo '<p><a href=""><div class="namen">'.$row->naam.'</div></a></p>';


}
}




if ($doen3 !=NULL){


$ztest = mysql_query("SELECT * FROM $doen3");




while ($row = mysql_fetch_object($ztest)){
echo '<p><a href=""><div class="namen">'.$row->naam.'</div></a></p>';


}

}


?>




</div>




</div>

<div class="mailtitel">


<div class="mailtiteltitel"><h1 class="hmailvan">Onderwerp:</h1></div>


<div class="mailtitelform">

<?php
if(isset($_POST['variable'])){
echo 'DERI '.$_POST['variable'];
}

?>
</div>


</div>


<div class="mailbericht">

<div class="mailberichttitel"><h1 class="hmailvan">Bericht:</h1></div>

<div class="mailberichttitelform">


<textarea class="comment" name="comments">




</textarea>


</div>

</div>


<?php

$myboxes = $_POST['comments']; // this works ok

?>


<div class="mailsend">

<div class="poper"><input type="submit" name="submit" value="Submit"></div>


</div>


</form>



</div>



</body>




and a event handler like this,




<?php
if(isset($_POST['submit'])){
$to = "my.email@host.com"; // is replaced with my real email
$from = $_POST[$van]; // this is the problem
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "DERI";
$subject2 = "Copy of your form submission";

$message = $from . " schreef volgend bericht:" . "\n\n" . $_POST['comments'];


$headers = "From:" . $from;

mail($to,$subject,$message,$headers);

echo "Mail werd met succes verstuurd.";

}
?>



Trying for 2 days now but no luck.

Bicklo
06-21-2017, 09:31 PM
The drop-down list is a list of email addresses from a database, so what i want is to echo or $_POST the selected email address on submit.

Hope to be a little clearer.