我是php的新手,似乎无法理解我的代码中的问题:
<?php
require('mysql_connect.php');
// If the values are posted, insert them into the database.
if (isset($_POST['username']) && isset($_POST['password'])&& isset($_POST['phone'])){
$username = $_POST['username'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$password = $_POST['password'];
$query = "INSERT INTO `user` (username, password, email,phone) VALUES ('$username', '$password', '$email', '$phone')";
$result = mysql_query($query);
if($result){
$msg = "User Created Successfully.";
}
echo "
<script >
function changeIT() {
$( '#phone,#reg' ).css('display','inline');
}
$(document).ready(changeIT);
</script>";
header('Location: http://atestat-cpi.comeze.com/index.html');
}
?>
根据我对php的理解,这应该执行jquery来显示所需的元素,然后显示带有显示元素的index.html。问题是,当页面被加入时,它什么都不显示,只是空白窗口,所以我认为jQuery没有执行。请帮助。
答案 0 :(得分:1)
header('Location: /')
进行重定向,因此不会发生任何事情。您必须重新排序代码和文件。我将通过一个示例向您展示一种继续进行的方式。
首先,让我们准备你的索引文件。我将直接使用ajax而不是典型的表单提交。 这是一个例子:
<!DOCTYPE html>
<html>
<head>
<!-- load jQuery -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- call to your own js -->
<script type="text/javascript" src="register_user.js"></script>
</head>
<body>
<!-- Instead of create a form you can do so -->
<div id="register">
<label><input id="user" type="text" value="" /></label>
<label><input id="pass" type="password" value="" /></label>
<label><input id="phone" type="phone" value="" /></label>
<label><input id="email" type="email" value="" /></label>
<input id="register_user" type="button" value="register!" />
</div>
</body>
</html>
现在,让我们设置您的javascript文件,我称之为register_user.js
:
// on dom ready
$(function(){
$('#register_user').on('click', function(){
// disable button, prevent multiple calls
var $this = $(this);
$this.prop('disabled', true);
$.ajax({
url: 'register.php',
type: 'post',
data: {
username: $('#user').val(),
password: $('#pass').val(),
phone: $('#phone').val(),
email: $('#email').val()
},
success: function(data){
var response = data.split('|');
if( response[0] == '1' ){
// this is part of your code, it should be in your index file
$('#phone, #reg').css('display', 'inline');
// restore the button (if you want) or whatever you want to do .)
$this.prop('disabled', false);
}
else{
alert(response[1]);
}
}
});
});
});
现在让我们制作你的php用户注册脚本。这应该是一个独立的脚本,我称之为register.php
:
<?php
$responses = array(
'-1|One or more required fields were not filled',
'0|There was an error creating the user. Try again.',
'1|Your user account was created successfully!'
);
if( isset($_POST['username'], $_POST['password'], $_POST['phone'], $_POST['email']) ){
require_once 'mysql_connect.php';
$username = $_POST['username'];
$password = $_POST['password'];
$phone = $_POST['phone'];
$email = $_POST['email'];
// You MUST use mysqli, but now let's use your current code
$query = "INSERT INTO `user` (username, password, email,phone) VALUES ('$username', '$password', '$email', '$phone')";
$result = mysql_query($query);
// If the user was created successfully, say ok.
// The usually is to say yes or no (1 or 0) and manage the message with JS in the view.
if( $result ) echo responses[2];
else echo responses[1];
exit();
}
echo responses[0];
exit();
?>
PS:我没有测试这段代码,所以它可能有失败或需要修复的东西。不记得它只是一个例子。
答案 1 :(得分:0)
试试这样..
<?php
//php code..
?>
<script>
<!--your script-->
</script>
<?php
//php code..
?>
如果您想要jQuery
工作,则还应包括jquery library
。
答案 2 :(得分:0)
键入header('Location: http://atestat-cpi.comeze.com/index.html');
后,您将被重定向到该uri,因此您的回音无效。此外,您似乎不包括JQuery,您可以使用CDN https://jquery.com/download/#using-jquery-with-a-cdn包含它。此代码中也存在SQL注入的主要问题。您应该使用mysql_real_escape_string(input)
方法转义用户输入。