【发布时间】:2016-05-07 22:04:45
【问题描述】:
我有一个与此线程中的服务非常相似的服务:
Php: Form auto-fill using $_SESSION variables with multiple php files
我会在那里问,但由于我没有 50 声望,我将不得不问一个新问题。
为了更好地理解 Ajax,我想重新创建 rkmax 的文件,看看它们是否可以工作。所以我将它们保存为 5 个单独的文件。
SESSION 似乎没有存储任何发布的信息。添加了print_r($_SESSION); 以跟踪当前其中的内容。此外,通过电话号码检索帐户信息的.blur 事件也不起作用。
这几天一直把我的头撞在墙上。当通过 Apache/XAMPP 在本地托管或在实际的 Web 服务器上工作时,它将不起作用。所有 5 个文件都在同一个文件夹中,并且标题与 rkmax 的文件标题完全相同。
我了解每个功能背后的逻辑,似乎无法在任何地方找到问题。我对编码很陌生,所以它很容易成为文件结构或我自己计算机的设置之类的东西?
阅读了一堆有类似问题的其他 StackOverflow 线程,但似乎没有一个适用。
感谢您的宝贵时间。
这是从 rkmax 的代码中复制的所有内容:
index.php
<?php
session_start();
if (!isset($_SESSION['customers'])) {
$_SESSION['customers'] = array(
'1234567' => '{"lname": "Berg", "mi": "M", "fname": "Thomas", "account": "1234"}',
'1122334' => '{"lname": "Jordan", "mi": "C", "fname": "Jacky", "account": "4321"}',
);
}
require __DIR__ . '/index_template.php';
index_template.php
<!doctype html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.js"></script>
<script src="scripts.js"></script>
</head>
<body>
<div style="margin-left: 300px">
<form id="dataForm" method="post">
<fieldset>
<legend>User info</legend>
<label for="fname">First name</label>
<input id="fname" type="text" name="fname" placeholder="First name"/>
<label for="mi">Middle inicial</label>
<input id="mi" type="text" name="mi" placeholder="Middle Initial"/>
<label for="lname">Last name</label>
<input id="lname" type="text" name="lname" placeholder="Middle Initial"/>
<label for="phone">Phone number</label>
<input id="phone" type="text" name="phone" placeholder="000000"/>
</fieldset>
<fieldset>
<legend>Account info</legend>
<label for="account">Account</label>
<input id="account" type="text" name="account"/>
</fieldset>
<input type="submit" name="submit"/>
<input type="reset" name="clear"/>
</form>
</div>
</body>
</html>
postCustomerInformation.php
session_start();
// example: converts $_POST['phone'] into $post_phone if exists
extract($_POST, EXTR_PREFIX_ALL, 'post');
// Validates that all required information was sent
if (isset($post_lname) && isset($post_fname) && isset($post_phone) && isset($post_account)) {
$customer = array(
'fname' => $post_fname,
'lname' => $post_lname,
'account' => $post_account,
'mi' => isset($post_mi) ? $post_mi : '' // optional
);
$_SESSION['customers'][$post_phone] = json_encode($customer);
// returns a valid json format header
header('Content-Type: application/json');
header("HTTP/1.0 204 No Response");
} else {
// returns error
header('Content-Type: application/json');
header("HTTP/1.0 400 Bad Request");
}
getCustomerInformation.php
session_start();
// example: converts $_GET['phone'] into $get_phone if exists
extract($_GET, EXTR_PREFIX_ALL, 'get');
if (isset($get_phone) && isset($_SESSION['customers'][$get_phone])) {
header('Content-Type: application/json');
echo $_SESSION['customers'][$get_phone];
} else {
header('Content-Type: application/json');
echo '{}';
}
scripts.js
;(function () {
"use strict";
function getCustomerInformation() {
var phone = jQuery(this).val();
if (!phone) {
return;
}
jQuery.ajax({
type: 'get',
url: 'getCustomerInformation.php',
data: {
phone: phone
},
success: function getCustomerInformation_success(data) {
// for each returned value is assigned to the field
for (var i in data) {
if (data.hasOwnProperty(i)) {
$('#' + i).val(data[i]);
}
}
}
});
}
function postCustomerInformation(event) {
event.preventDefault();
var form = jQuery(this);
jQuery.ajax({
type: 'post',
url: 'postCustomerInformation.php',
data: form.serializeArray(),
success: function postCustomerInformation_success() {
alert("OK");
},
error: function postCustomerInformation_error() {
alert("Error");
}
})
}
// set behaviors when document is ready
jQuery(document).ready(function document_ready() {
jQuery('#phone').blur(getCustomerInformation);
jQuery('#dataForm').submit(postCustomerInformation);
});
})();
【问题讨论】:
-
如果您在帖子中包含至少一个 Ajax 脚本、匹配的 php 脚本以及它的 HTML 标记,对这里的每个人来说都会容易得多。
-
当然没问题。我认为这应该就是一切。
-
您是否遇到任何控制台错误?
-
没有,我不能出现任何错误。有没有人尝试过这段代码并且对他们有用?
-
我正在构建一个进行测试。你所拥有的这个对于它想要做的事情来说似乎过于复杂(无论如何我都理解)。
标签: javascript php jquery ajax session