我正在尝试保存会话中输入字段的值,因此当用户无法完成表单验证时,这些值将显示在会话的输入字段中。
这是我的代码的外观:
function data_setter($name) {
if (isset($_SESSION[$name])) {
return $_SESSION[$name];
}
}
function show_form() {
return '
<h1>E-Mail-Vorbereitung<br /><br /></h1>
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<table>
<tr>
<td><label for="anrede">Anrede</label></td>
<td>
<select name="anrede" id="anrede" class="staff_form">
<option id="select" disabled selected> -- Wähle eine Option -- </option>
<option id="frau" value="frau">Frau</option>
<option id="herr" value="herr">Herr</option>
</select>
</td>
</tr>
<tr>
<td><label for="name">Name</label>
<td><input value="' . data_setter('receiver') . '" type="input" class="staff_form" name="receiver" id="name" placeholder="Name des Empfängers eingeben" /></td>
</tr>
<tr>
<td><label for="email">E-Mail</label></td>
<td><input value="' . data_setter('email') . '" type="input" class="staff_form" name="email" id="email" placeholder="E-Mail des Empfängers eingeben" /></td>
</tr>
</table>
<br /><input name="sent" type="submit" value="Daten prüfen" />
</form>
';
}
function mailsender() {
if (isset($_POST['sent']) &&
isset($_POST['anrede']) && !empty($_POST['anrede']) &&
isset($_POST['receiver']) && !empty($_POST['receiver']) &&
isset($_POST['email']) && !empty($_POST['email'])
) {
if (!is_email($_POST['email'])) {
$_SESSION['receiver'] = $_POST['receiver'];
$_SESSION['email'] = $_POST['email'];
var_dump($_SESSION);
echo '
<h1>Error</h1>
<p>Die eingegebene E-Mail-Adresse ist nicht gültig. Hast du dich vielleicht vertippt?</p>
<FORM>
<INPUT TYPE="submit" onClick="window.location.href = "' . $_SERVER['REQUEST_URI'] . '";" VALUE="Nochmals probieren">
</FORM>
';
}
else {
// Delete sessions
if (isset($_SESSION['anrede'])) { unset($_SESSION['anrede']); }
if (isset($_SESSION['receiver'])) { unset($_SESSION['receiver']); }
if (isset($_SESSION['email'])) { unset($_SESSION['email']); }
global $wpdb;
$table_name = "yc_customers";
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") == $table_name) {
$myrows = $wpdb->get_results( "SELECT email FROM yc_customers" );
foreach ($myrows as $myrow) {
// Doing sumth here
}
}
echo '
<h1>Daten valid</h1>
<p>Super, die Daten wurden geprüft und die Firma ist in der Your-Click-Datenbank <strong>nicht vorhanden</strong>.</p>
';
}
}
else {
// Set sessions
if (isset($_POST['receiver']) && !empty($_POST['receiver'])) { $_SESSION['receiver'] = $_POST['receiver']; }
if (isset($_POST['email']) && !empty($_POST['email'])) { $_SESSION['email'] = $_POST['email']; }
// Validating
if ($_POST['sent']) {
// Anrede
if (!isset($_POST['anrede']) || empty($_POST['anrede'])) {
echo '
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Bitte wähle eine Anrede aus.
</div>
';
}
// Receiver
if (!isset($_POST['receiver']) || empty($_POST['receiver'])) {
echo '
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Bitte gebe den Namen des Empfängers ein.
</div>
';
}
// E-Mail
if (!isset($_POST['email']) || empty($_POST['email'])) {
echo '
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">Error:</span>
Bitte gebe die E-Mail-Adresse des Empfängers ein.
</div>
';
}
}
echo show_form();
var_dump($_SESSION);
// Set the chosen option
if (isset($_POST['anrede']) && !empty($_POST['anrede'])) {
echo '<script>$("#select").prop("selected", false);</script>';
if ($_POST['anrede'] == 'frau') {
echo '<script>$("#frau").prop("selected", true);</script>';
}
elseif ($_POST['anrede'] == 'herr') {
echo '<script>$("#herr").prop("selected", true);</script>';
}
}
}
}
add_shortcode('mailsender', 'mailsender');
当我在电子邮件字段中输入任何内容并且电子邮件无效时,var_dump会正确显示所有会话,但是当我点击按钮时,#Nochmals probieren&#34;回到表单页面,var_dump告诉我NULL。我的会议在哪里?
答案 0 :(得分:2)
你需要在某处调用session_start()。而且你的data_setter函数有点没用,因为你可以直接执行$ _SESSION ['whatever']。
答案 1 :(得分:1)
你有没有开始你的会话session_start();
答案 2 :(得分:1)
我似乎只在您的表单页面中使用session_start()。在任何代码开始提交表单之前,请在PHP页面顶部的页面中使用session_start()
。我希望这对你有帮助。
答案 3 :(得分:0)
嗯,对于completness,我的评论作为答案:在您第一次尝试从会话中保存/读取数据之前,您错过了一次session_start()调用(通常是您的入口点脚本的第一个设置步骤之一)。
答案 4 :(得分:0)
在所需页面中启动会话,session_start();