如果有人可以帮我解决这个问题,我真的很感激 - 我几乎让它工作了!
基本上,我正在制作自己的应用程序想法一段时间,我之所以这样做是因为我不熟悉编码,所以我觉得它会教会我很多不同的元素(即添加,更新)并删除记录)。
因此,通过此示例,我有一个用户成功登录,并且正在创建一个正在沿其他表单传递的会话。我希望他们能够增加一笔交易 - 我几乎有这笔交易,但似乎很难与一部分斗争。
我的HTML代码是:
<!DOCTYPE html>
<html>
<head>
<title>Find A Deal</title>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
<link rel="stylesheet" href="http://localhost/findadeal/themes/deal.css" />
<style>
#login-button {
margin-top: 30px;
}
</style>
<script src="http://www.dragan-gaic.info/js/jquery-1.8.2.min.js"></script>
<script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
<script src="js/custom3.js"></script>
<script src="js/custom4.js"></script>
<?php
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
/* User is logged in */
echo "IT WORKS!";
} ?>
</head>
<body>
<div data-role="page" id="login">
<div data-theme="a" data-role="header">
<h3>Find A Deal</h3>
</div>
<div data-role="content">
<label for="username">Enter your username:</label>
<input type="text" value="" name="username" id="username"/>
<label for="password">Enter your password:</label>
<input type="password" value="" name="password" id="password"/>
<a data-role="button" id="login-button" data-theme="b">Login</a>
</div>
<div data-theme="a" data-role="footer" data-position="fixed">
</div>
</div>
<!--Newly rendered page after successful login!-->
<div data-role="page" id="index">
<div data-theme="a" data-role="header">
<h2>Find A Deal</h2>
</div>
<div data-role="content">
<?php
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
echo "IT WORKS!";
} ?>
<h3></h3>
<a href="#view" data-role="button" data-icon="search">View Deals</a>
<a href="#add" data-role="button" data-icon="plus">Add Deals</a>
</div>
</div>
<!--View Deal Page-->
<div data-role="page" id="view">
<div data-role="header">
<h2>Find A Deal</h2>
</div>
<div data-role="content">
<?php
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
echo "IT WORKS!";
} ?>
<h3></h3>
<p>test so I know I'm onto a new page</p>
<a href="#view" data-role="button" data-icon="search">View Deals</a>
<a href="#add" data-role="button" data-icon="plus">Add Deals</a>
</div>
<div data-role="footer">
</div>
</div>
<!--Add Deal Page-->
<div data-role="page" id="add">
<script src="js/custom4.js"></script>
<div data-role="header">
<h2>Find A Deal</h2>
</div>
<div data-role="content">
<?php
if( !isset( $_SESSION ) ){
session_start();
}
if( isset( $_SESSION['username'] ) ){
echo "It's working!";
} ?>
<label for="name">Deal Name:</label>
<input type="text" value="" name="name" id="name"/>
<label for="desc">Description</label>
<input type="text" value="" name="desc" id="desc"/>
<a data-role="button" id="submit-button" data-theme="b">Submit</a>
</div>
</div>
</body>
</html>
所以只是为了解释一下 - 第一页是登录页面,它会将您带到索引旁边,这只是我设置的菜单页面,然后从菜单中选择2页中的1页 - 查看交易和添加交易。我现在正在处理添加交易。
添加交易时,用户会在相关文本框中输入交易名称和描述,以便将其添加到数据库中。 我正在使用这个JS函数:
//Adding a new deal
$(document).on('pagebeforeshow', '#add', function(){
$('#submit-button').on('click', function(){
if($('#name').val().length > 0 && $('#desc').val().length > 0){
userObject.name = $('#name').val(); // Put username into the object
userObject.desc = $('#desc').val(); // Put password into the object
// Convert an userObject to a JSON string representation
var outputJSON = JSON.stringify(userObject);
// Send data to server through ajax call
// action is functionality we want to call and outputJSON is our data
ajax.sendRequest({action : 'add', outputJSON : outputJSON});
} else {
alert('Please fill all nececery fields');
}
});
});
$(document).on('pagebeforeshow', '#index', function(){
if(userObject.name.length == 0){ // If username is not set (lets say after force page refresh) get us back to the login page
$.mobile.changePage( "#add", { transition: "slide"} ); // In case result is true change page to Index
}
$(this).find('[data-role="content"] h3').append('Deal Added:' + userObject.name); // Change header with added message
//$("#index").trigger('pagecreate');
});
// This will be an ajax function set
var ajax = {
sendRequest:function(save_data){
$.ajax({url: 'http://localhost/findadeal/login/json3.php',
data: save_data,
async: true,
beforeSend: function() {
// This callback function will trigger before data is sent
$.mobile.showPageLoadingMsg(true); // This will show ajax spinner
},
complete: function() {
// This callback function will trigger on data sent/received complete
$.mobile.hidePageLoadingMsg(); // This will hide ajax spinner
},
success: function (result) {
if(result == "true") {
$.mobile.changePage( "#index", { transition: "slide"} ); // In case result is true change page to Index
} else {
alert('Deal Addition has been unsuccessful, please try again!'); // In case result is false throw an error
}
// This callback function will trigger on successful action
},
error: function (request,error) {
// This callback function will trigger on unsuccessful action
alert('Error has occurred, please try again!');
}
});
}
}
// We will use this object to store username and password before we serialize it and send to server. This part can be done in numerous ways but I like this approach because it is simple
var userObject = {
name : "",
desc : ""
}
然后将其转换为PHP文件,准备将输入写入数据库,如下所示:
<?php
$var1 = $_REQUEST['action']; // We dont need action for this tutorial, but in a complex code you need a way to determine ajax action nature
$jsonObject = json_decode($_REQUEST['utputJSON']); // Decode JSON object into readable PHP object
$name = $jsonObject->{'name'}; // Get name from object
$desc = $jsonObject->{'desc'}; // Get desc from object
mysql_connect("localhost","root",""); // Conect to mysql, first parameter is location, second is mysql username and a third one is a mysql password
@mysql_select_db("findadeal") or die( "Unable to select database"); // Connect to database called test
$query = "INSERT INTO deal (dname, description) VALUES ('$name' ,'$desc')";
$result=mysql_query($query);
$num = mysql_numrows($result);
if($num != 0) {
echo "true";
} else {
echo "false";
}
?>
这一切都运行正常,但是,数据未插入数据库的原因是因为它需要用户的ID,其中用户名存储在会话变量中。
我想要帮助的是:
1。有人可以告诉我如何从会话变量获取数据并将其带到PHP文件中。 2。此外,会话变量存储用户名 - 有没有办法可以自行检查其相关的userid标签,还是必须通过PHP中的SQL语句来完成文件。
我希望我没有引起任何混淆,我已经清楚地知道我遇到了什么问题。我是学生,所以在学习过程中都会感激所有的帮助!如果会话数据像其他数据一样通过javascript函数或是否能够直接读取到编写PHP文件或者什么,我会感到困惑? = /
感谢您的时间!
答案 0 :(得分:0)
如果我理解得很好,你现在对学习东西更感兴趣,而不是创建应用程序。我建议你一次尝试学习一件事,在单独的测试文件中,当你获得必要的知识,然后将所有应用程序组合在一起,让你学到的一切都应用到现实生活中。
我是一名教练,培养了很多学生,并帮助他们实现了一些他们可以引以为傲的产品,所以如果你有兴趣逐一学习,我想我可以帮助你,如果你等。
答案 1 :(得分:0)
如果我的逻辑正确,你必须做这样的事情:
在您的登录HTML / PHP页面上以:
开头<?php
if( !isset( $_SESSION ) ){ session_start(); }
?>
THE REST OF YOUR CODE FOLLOWS HERE
在您的登录操作PHP脚本中,再次:
<?php
if( !isset( $_SESSION ) ){ session_start(); }
// now you look up your user - how ever you do this...
// might be something like this...
// this is not a real function it should just demonstrate
// the possible logic.
if(!isset($_SESSION['userid'])) {
// you just have to check in case no session userid is set:
function getUser( $username, $password ) {
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$sql = 'SELECT id FROM users WHERE username=:username AND pwd=:pwd';
$statement = $db->prepare($sql);
$statement->bindParam(':username', $username, PDO::PARAM_STR);
$statement->bindParam(':pwd', $password, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if(!empty($result)) {
return $result['id'];
}
return false;
}
// call your login logic (here getUser) and
// if you have a result, set the session...
$userExists = getUser($_POST['user'], $_POST['pwd']);
if($userExists) {
$_SESSION['userid'] = $userExists;
}
}
?>
在交易行动中,再次:
<?php
if( !isset( $_SESSION ) ){ session_start(); }
// you check if the username session exists
if(isset($_SESSION['userid'])) {
// in case you have...
// proceed with your deal logic
// and use $_SESSION['userid'] to fill it to the DB
}
?>