Reading a session variable to write to the database

If someone could help me out with this I'd really appreciate it - I almost have it working! Basically, I'm making my own application idea for a while, the reason I'm doing it is because I am new to coding, so I felt it would teach me a lot about the different elements (i.e. adding, updating and deleting records). So with this example, I have a user logging in successfully and it's creating a session which is being passed along the other forms. I want them to be able to add a deal - which I almost have but seem to struggle with one part. My HTML code is: Find A Deal

Find A Deal

Login

Find A Deal

Find A Deal

test so I know I'm onto a new page

View Deals Add Deals

Find A Deal

Submit
So just to explain that - the first page is a login page, it brings you next to index which is just a menu page I've set up, then from the menu you can select 1 of 2 pages - view deal and add deal. I'm working with the add deal one at the minute. When adding a deal the user inputs a deal name and a description into the relevant text boxes so it can be added over to the database. I am using this JS function: //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 : "" } This will then be fed over to a PHP file, ready to write the input to the database like this: {'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"; } ?> This is all working fine, however, the reason the data is not being inserted to the database is because it needs an ID from the user of which the username is being stored in the session variable. What I would like help with is: **1.** Can someone show me how you get the data from the session variable and have it brought over to the PHP file. **2.** Also, the session variable is storing the username - is there a way I can go about checking for its relevant userid tag on its own or will this have to be done through the SQL statement in the PHP file. I've hope I've not caused any confusion and I've made myself clear about what it is I'm having trouble with. I am a student so all help would be appreciated in the learning process! I'm getting confused in thinking should the session data go though the javascript function like the other data or should it be able to read straight over to the writing PHP file or what? =/ Thanks for your time!
You can't handle sessions properly in case you already had HTML output. Be sure that you start session (on each page) before you write any line of HTML

以上就是Reading a session variable to write to the database的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏