переменные — передача строк (адрес электронной почты) из формы в переполнение стека

У меня есть контактная форма php, и все отлично работает с одним адресом, но я пытаюсь изменить свой сценарий для обработки выпадающего селектора, который позволяет выбрать получателя (адрес электронной почты для отправки).

Вот часть кода, которую я до сих пор пытался решить с этой проблемой:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="recipient" id="recipient">
<p class="text">
Please select recipient<br>
<select name="recipient" size="4"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['recipient'], ENT_QUOTES).'"';
} ?>
>
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</p>
</form>

<?php if (array_key_exists('send', $_POST)) {
// mail processing script
if ('recipient' == 1) {
$to = 'soandso@mail.com';
}
elseif('recipient' == 2) {
$to = 'soandso@mail.com';
}
elseif('recipient' == 3) {
$to = 'soandso@mail.com';
}
else {
echo 'Sorry for no recipient';
}

//then remainder code to process the rest which works fine

Я уверен, что моя проблема заключается в вызове / получении значения получателя, но я не могу понять, куда идти дальше.

0

Решение

Вы пытаетесь сделать что-то странное здесь. Так должно быть:

if ($_POST['recipient'] == 1) {
$to = 'soandso@mail.com';
}
else if($_POST['recipient'] == 2) {
$to = 'soandso@mail.com';
}
else if($_POST['recipient'] == 3) {
$to = 'soandso@mail.com';
}
else {
echo 'Sorry for no recipient';
}

Конечно 'recipient' никогда не будет равен 1, 2 или 3.

Я также заметил form и select имеет то же имя «получатель». Я не знаю, это проблема, хотя. Но я все равно хотел бы заняться этим.

1

Другие решения

Этот код работает на 100%:

(function($) {

$('#recipient').on('click', function() {
$('#recipient-form').submit();
});

})(jQuery);
<div id="page">
<?php

$to = '';

if (isset($_POST['recipient'])) :// mail processing script
if ($_POST['recipient'] == 1) {
$to = 'recipient1';
}
else if($_POST['recipient'] == 2) {
$to = 'reciipient2';
}
else if($_POST['recipient'] == 3) {
$to = 'recipient3';
}
else {
$to = 'Sorry for no recipient';
}

echo $to;
else : ?><form action="" method="post" id="recipient-form">
<select id="recipient" name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</form>
</div><?php endif; ?>
0

Надеемся, что страница целиком (в основном) целиком пояснена:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="recipient-form">
<p class="text">
Please select recipient<br>
<select id="recipient" name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
</p>
</form>

<?php if (array_key_exists('send', $_POST)) {
// mail processing script
$to = '';

if ($_POST['recipient']) {// mail processing script
if ($_POST['recipient'] == 1) {
$to = '';
}
else if($_POST['recipient'] == 2) {
$to = '';
}
else if($_POST['recipient'] == 3) {
$to = '';
}
else {
$to = 'Sorry for no recipient';
}
}

$subject = 'Feedback From Website';

// list expected fields
$expected = array('name', 'email', 'comments', 'subscribe');
// set required fields
$required = array('name', 'email', 'comments');

// set additional headers
$headers = 'From: ';

// set the include
$process = 'includes/process.inc.php';
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
mail($me, 'Server Problem', "$process cannot be read", $headers);
}
}
?>

<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && $link) {
?>
<p class="warning">Sorry, Messages sent that contain links will not be sent.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try again later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="success">Your message has been sent. Thank you for your message!</p>
<?php } ?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact" id="contact" onSubmit="MM_validateForm('name','','R','email','','RisEmail','comments','','R');return document.MM_returnValue">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" type="text" class="textInput" id="name"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" type="text" class="textInput" id="email"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="comments">Message:<?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your message</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="45" rows="5"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments'], ENT_QUOTES);
} ?></textarea>
</p>
<p>

<p class="text">
Please check the box if you would like to sign up for our Mailing List!
<input type="checkbox" name="subscribe" value="Yes"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['subscribe'], ENT_QUOTES).'"';
} ?>
>
</p>

<p>
<?php
require_once('recaptchalib.php');
$publickey = "6Lf3NdQSAAAAAOAwgPGRybLnY175X6k9PJ1F2vHx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</p>
<p class="last">
<input type="submit" name="send" id="send" value="Send Message">
</p>
</form>

Надеюсь, что все это сейчас поможет кому-то найти лучшее решение!

0

Целостность снова. Я думаю / надеюсь, что мы становимся ближе:

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="getEmail">
<p class="text">Please select recipient</p><br>
<select name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
<input type='hidden' name='do' value='1'>
</form>

<?php
if (array_key_exists('send', $_POST)) {
if (isset($_POST['do'])) {
// mail processing script
if ($_POST['recipient'] == 1) { $to = ''; }
else if($_POST['recipient'] == 2) { $to = ''; }
else if($_POST['recipient'] == 3) { $to = ''; }
else echo 'Sorry for no recipient';
}
echo $to;$subject = 'Feedback From Website';

// list expected fields
$expected = array('name', 'email', 'comments', 'subscribe');
// set required fields
$required = array('name', 'email', 'comments');

// set additional headers
$headers = 'From:';

// set the include
$process = 'includes/process.inc.php';
if (file_exists($process) && is_readable($process)) {
include($process);
}
else {
$mailSent = false;
mail($me, 'Server Problem', "$process cannot be read", $headers);
}
}
?>

<?php
if ($_POST && isset($missing) && !empty($missing)) {
?>
<p class="warning">Please complete the missing item(s) indicated.</p>
<?php
}
elseif ($_POST && $link) {
?>
<p class="warning">Sorry, Messages sent that contain links will not be sent.</p>
<?php
}
elseif ($_POST && !$mailSent) {
?>
<p class="warning">Sorry, there was a problem sending your message. Please try again later.</p>
<?php
}
elseif ($_POST && $mailSent) {
?>
<p class="success">Your message has been sent. Thank you for your message!</p>
<?php } ?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="contact" id="contact" onSubmit="MM_validateForm('name','','R','email','','RisEmail','comments','','R');return document.MM_returnValue">
<p>
<label for="name">Name: <?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Please enter your name</span><?php } ?>
</label>
<input name="name" type="text" class="textInput" id="name"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['name'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="email">Email: <?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Please enter your email address</span><?php } ?>
</label>
<input name="email" type="text" class="textInput" id="email"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['email'], ENT_QUOTES).'"';
} ?>
>
</p>
<p>
<label for="comments">Message:<?php
if (isset($missing) && in_array('comments', $missing)) { ?>
<span class="warning">Please enter your message</span><?php } ?>
</label>
<textarea name="comments" id="comments" cols="45" rows="5"><?php
if (isset($missing)) {
echo htmlentities($_POST['comments'], ENT_QUOTES);
} ?></textarea>
</p>
<p>

<p class="text">
Please check the box if you would like to sign up for our Mailing List!
<input type="checkbox" name="subscribe" value="Yes"<?php if (isset($missing)) {
echo 'value="'.htmlentities($_POST['subscribe'], ENT_QUOTES).'"';
} ?>
>
</p>

<p>
<?php
require_once('recaptchalib.php');
$publickey = "6Lf3NdQSAAAAAOAwgPGRybLnY175X6k9PJ1F2vHx"; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</p>
<p class="last">
<input type="submit" name="send" id="send" value="Send Message">
</p>
</form>
0

Ваше имя формы и выбранное имя совпадают. также вы повторяете значение сообщения внутри выбора. Я думаю, что это осталось от вашего предыдущего поля ввода.

попробуй это;

<form action="" method="post" id="getEmail">
<p class="text">Please select recipient<br></p>
<select name="recipient" size="4">
<option value="">Select...</option>
<option value="1">Artistic Director</option>
<option value="2">Site Administrator</option>
<option value="3">Someone else</option>
</select>
<input type='hidden' name='do' value='1'>
<input type='sumbit' value='Go'>
</form>

<?php if (isset($_POST['do'])) {
// mail processing script
if ($_POST['recipient'] == 1)$to = 'email1';
else if($_POST['recipient'] == 2)$to = 'email2';
else if($_POST['recipient'] == 3)$to = 'email3';
else echo 'Sorry for no recipient';
}
//echo $to;
//to send mail
$sub = 'Mail from web Form';
$msg = 'My message';
$mail_status= mail($to, $sub, $msg);
if($mail_status){do something on success}; else {do something on failure};
?>
0
По вопросам рекламы ammmcru@yandex.ru
Adblock
detector