誰かがこれを手伝ってくれるなら、私は本当に感謝しています-私はほとんどそれを機能させています!
基本的に、私はしばらくの間自分のアプリケーションのアイデアを作成していますが、それを行う理由は、コーディングに慣れていないためです。そのため、さまざまな要素(つまり、レコードの追加、更新、削除)について多くのことを学ぶことができると感じました。 )。
したがって、この例では、ユーザーが正常にログインし、他のフォームに渡されるセッションを作成しています。私は彼らに取引を追加できるようにしたいと思っています-私はほとんど持っていますが、一部で苦労しているようです。
私の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.また、セッション変数はユーザー名を格納しています-関連するユーザーIDタグを自分でチェックする方法はありますか、それともPHPファイルのSQLステートメントを使用して行う必要がありますか。
混乱が生じていないことを願っており、問題が何であるかを明確にしています。私は学生ですので、学習の過程ですべての助けをいただければ幸いです。セッションデータが他のデータと同じようにjavascript関数を通過するのか、それとも書き込み中のPHPファイルに直接読み取ることができるのか、という考えに混乱しています。= /
御時間ありがとうございます!