我正在尝试从我们从index.php开始的上一页调用我的$ _POST表单信息的某些部分,将表单以POST形式发送到contact.php,然后将其发送给它(该部分工作)虽然)。我遇到麻烦的是将帖子信息放回会话变量并将其发送回index.php。我可以为$ _SESSION ['pos']做一个foreach命令,然后一个接一个地打印出每个变量,但它让我无法在他们的信息之前输入东西,比如名字:和电子邮件:
contact.php
session_start(); //start session
$_SESSION['pos'] = $_POST; // turn $_POST info into $_session info
的index.php
if(isset($_SESSION['pos'])){ //check to see if session info is set
echo '<div class="alert alert-info">'; //create alert div
$item = $_SESSION['pos']; //change session into variable for easier indexing?
echo "Name: ". $item[0]."<br><br>"; //echo line 0 from array
echo "Email: ". $item['1']."<br><br>"; //echo line 1 from array
echo "</div>"; //close alert div
}
session_unset();//unset session variables to avoid re-using them.
我真的希望我知道如何正确搜索我正在寻找的答案。我不得不求助于如何做到这一点,我觉得很可怕,因为我确定那里的答案。
答案 0 :(得分:2)
除非您的表单字段名为<div id="details" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h2 id="detailsHeader"></h2>
</div>
<div class="modal-body">
<div id="detailsBody" ></div>
</div>
<div class="modal-footer">
<a href="#" id="btnModalCancel" class="btn" data-dismiss="modal">Close</a>
<input type="submit" id="btnModalSave" class="btn btn-primary" value="Save"/>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function () {
$.ajaxSetup({ cache: false });
initialiseModals();
});
function initialiseModals() {
$('#details').on('shown.bs.modal', function () {
$.validator.unobtrusive.parse($('#details'));
});
$(document).on('click', '*[data-modalload]', function () {
var e = $(this);
if (e.data('submittext') != undefined) {
$('#btnModalSave').html(e.data('submittext'));
} else $('#btnModalSave').html('Save');
if (e.data('class') != undefined) {
var cls = e.data('class');
$('#details').removeClass(cls).addClass(cls);
}
if(e.data('modalsize') != undefined) {
var size = e.data('modalsize');
$('.modal-dialog').addClass('modal-' + size);
}
if (e.data('modalsubmit') == undefined) {
$('#btnModalSave').hide();
$('#btnModalCancel').addClass("btn-primary");
}
else {
$('#btnModalSave').show();
$('#btnModalCancel').removeClass("btn-primary");
$('#btnModalSave').unbind('click').click(function (ctrl) {
$('#btnModalSave').attr('disabled', 'disabled');
ctrl.preventDefault();
var submitUrl = $('#' + e.data('modalsubmit')).attr("action");
var formData = $('#' + e.data('modalsubmit')).serialize();
$.post(submitUrl,
formData,
function (data, status, xhr) {
$('#btnModalSave').removeAttr('disabled');
$('#details').modal('hide');
if (e.data('modalsuccess') != undefined) {
eval(e.data('modalsuccess'));
}
}).error(function () {
$('#btnModalSave').prop('disabled', false);
});
});
}
$('#detailsBody').load(e.data('modalload'), function () {
$('#detailsHeader').html(e.data('modaltitle'));
$('#details').modal('show');
$.validator.unobtrusive.parse($('#detailsBody'));
});
});
}
</script>
和0
等,否则1
不正确,应该类似Name: " . $item[0]
,其中数组键对应于表单输入&#39 ; s name属性。
此外,您可能希望避免调用Name: " . $item['name']
,因为这将清除您可能不需要的所有会话数据。
相反,您可以对相关数据使用unset()
:
session_unset()
如果你有:
// unset the POST data from the session, leave everything else
unset($_SESSION['pos']);
您的代码可能如下所示:
<input type="text" name="name" placeholder="Your Name">
<input type="text" name="email" placeholder="Email Address">