情况是这样的:有2个材料:regis和profil,有一个连接到regis的ajax脚本,有一个通过ajax发送到saveUser.php的表单处理程序。如果您使用以下代码,那么一切都会正常运行(所有检查都有效,一切都已保存,它会从 regis 切换到 profil)。
注册:
<div id="result_form"> </div>
<form id="reg_form" class="form" action="" method="POST">
<div class="form-group"><label>Ваш логин для входа на сайт:</label><br /> <input class="form-control" maxlength="50" name="login" type="text" placeholder="Your login..." data-validate="" /><span class="hint">Обязательно для заполнения</span></div>
<div class="form-group"><label>Ваш пароль:</label><br /> <input class="form-control" maxlength="50" name="password" type="password" placeholder="Your pASS..." data-validate="" /><span class="hint">Обязательно для заполнения</span></div>
<div class="form-group"><label>Ваша должность:</label><br /> <input class="form-control" maxlength="50" name="position" type="text" placeholder="Your position..." data-validate="" /><span class="hint">Обязательно для заполнения</span></div>
<div class="form-group"><label>Ваше место работы:</label><br /> <input class="form-control" maxlength="50" name="placeJob" type="text" placeholder="Your place job..." data-validate="" /><span class="hint">Обязательно для заполнения</span></div>
<div class="form-group"><input id="reg_btn" class="btn btn-primary" name="submit" type="submit" value="Зарегистрироваться" /></div></form>
profile - 这里应该通过 sourcerer 插件从会话中输出一个值
{source} <?php
echo("Hello");
?> {/source}
阿贾克斯:
$( document ).ready(function() {
$("#reg_btn").click(
function(ev){
ev.preventDefault();
var forma = $("#reg_form");
var field = [];
var leng = 0;
forma.find('input[data-validate]').each(function() {
field.push('input[data-validate]');
var value = $(this).val();
var line = $(this).closest('.form-group');
if(!value){
line.addClass('emptyField');
setTimeout(function() {
line.removeClass('emptyField')
}.bind(this),3000);
}else{leng++;}
});
if(leng == field.length){
sendAjaxForm('result_form', 'reg_form', 'http://ip/nameSite/templates/bootstrap4/dopScripts/saveUser.php');
return false;
}
}
);
});
function sendAjaxForm(result_form, reg_form, url) {
$.ajax({
url: url,
type: "POST",
dataType: "html", //формат данных
data: $("#"+reg_form).serialize(),
success: function(response) {
result = $.parseJSON(response);
if(result.already == "True"){
var message = document.getElementById('result_form');
message.innerHTML = "<p style='color: red;'>Такой логин уже существует</p>";
message.scrollIntoView();
}else{
window.location.href = 'moj-profil';
}
},
error: function(response) {
$('#result_form').html('Ошибка. Данные не отправлены.');
}
});
}
保存用户.php:
<?php
if (isset($_POST['login'])) { $login = $_POST['login']; if ($login == '') { unset($login);} }
if (isset($_POST['password'])) { $password = $_POST['password']; if ($password =='') { unset($password);} }
if (isset($_POST['position'])) { $position = $_POST['position']; if ($position =='') { unset($position);}}
if (isset($_POST['placeJob'])) { $placejob = $_POST['placeJob']; if ($placejob =='') { unset($placejob);}}
include('bd.php');
$checkUser="SELECT nick FROM registration WHERE nick = ?";
$query = $db->prepare($checkUser);
$query->bind_param("s", $login);
$query->execute();
$query->store_result();
$query->bind_result($check_email);
$query->fetch();
$empty = $query->num_rows();
if($empty != 0){
$already="True";
$result = array('already'=>$already);
}else{
$insert = "INSERT INTO registration (nick, pass, position, placejob)VALUES (?, ?, ?, ?)";
$qinsert = $db->prepare($insert);
$qinsert->bind_param("ssss", $login, $password, $position, $placejob);
$qinsert->execute();
$already="False";
//THIS PLACE
$result = array(
'already' => $already,
'login' => $login
);
}
echo json_encode($result);
?>
现在的问题。很多资料表明,session_start(); $_SESSION['']
jooml 中的常规构造会话不起作用,应使用以下构造:
报名参加会议
define( '_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$mainframe = JFactory::getApplication('site');
$session = JFactory::getSession();
$session->set('myVar', array('login'=>$login, 'first'=>1));
要获得会话:
{source} <?php
define( '_JEXEC', 1 );
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']);
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();
$session = JFactory::getSession();
$mySess = $session->get('myVar', array());
echo("Hello $mySess");//Результат Hello Array
foreach($mySess as $one){
echo("Hello".$one['login']." ".$one['first']);//Результат пуст
}
?> {/source}
但它不起作用,因为如果会话入口代码插入//THIS PLACE
到 saveUser.php 文件或任何其他地方,那么所有内容都将写入数据库,但 ajax 不会重定向到新页面并会说Uncaught SyntaxError: Unexpected end in JSON
,如果使用defined('_JEXEC') or die;
或Uncaught SyntaxError: Unexpected token < in JSON
,如果使用define('_JEXEC', 1);
。
我是joomle的新手,不太了解。您能告诉我如何修复此错误,以便会话正常连接、保存和工作吗?
事实证明,问题出在
define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT'])
. 文件路径指定不正确。我试图制作define('JPATH_BASE', dirname(dirname(__FILE__)) )
其他类似物,但他们指出了一个带有文件的文件夹,然后他们试图通过require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php')
. 没有这样的路径,您需要获取服务器的根并为此修复它:define('JPATH_BASE', $_SERVER['DOCUMENT_ROOT']."/nameSite")
. 好吧,这是最终的工作代码。要注册一个会话:
要获得会话: