Jump to content

helloworld001

Members
  • Posts

    90
  • Joined

  • Last visited

Everything posted by helloworld001

  1. I think I figured out what you were saying. Here is updated form with hidden record values that I need. <form class="new-form"> <textarea name="message" class="message">Enter message</textarea> <input type="hidden" name="recordid" class="record-id" value="<?php echo $record_id; ?>"> <input type="hidden" name="mssgto" class="mssg-to" value="<?php echo $user_id; ?>"> <input type="hidden" name="mssgby" class="mssg-by" value="<?php echo $mainUserid; ?>"> <input type="button" name="submit" class="form-submit" value="Send Message"> </form> Here is the process.php with the $_POST values. <?php require_once 'core/init.php'; $record_id = $_POST['recordid']; $mssg_to = $_POST['mssgto']; $mssg_by = $_POST['mssgby']; $message = $_POST['message']; $insertMssg = $db->prepare("INSERT INTO sub_records(record_id, message_to, message_by, message, bid_posted) VALUES(:record_id, :message_to, :message_by, :message)"); $insertMssg->bindParam(':record_id', $record_id); $insertMssg->bindParam(':message_to', $mssg_to); $insertMssg->bindParam(':message_by', $mssg_by); $insertMssg->bindParam(':message', $message); $resultInsert = $insertMssg->execute(); if($resultInsert == false) { $error = 'Your message could not be inserted.'; } else { $success = 'Your message has been added.'; } ?> <div class="success"><?php echo $success; ?></div> <div class="error"><?php echo $error; ?></div> Now I am a little lost on how to input ALL the form input values into jquery var like below. It already has 1 input value. How do I add the other 3? var myData = 'message='+ $(".message").val();
  2. Could you please provide me with a visual representation(using my code) with what you have just explained. I understand most of what you are saying, but I am unable to translate that into my code. A visual represenation of the code would be a lot more helpful. And yes I am only using session data on process.php page.
  3. So here's the situation. 1. I retrive records form mysql database. WORKS. 2. Within each record, I load a "form" via on click .button. WORKS. 3. The form is basically a way for a User to send a message to that record specifically. WORKS That process works. What doesn't work is that every time I submit a form to the database, it will not insert the "message" field text(appears empty) and the "record_id" will insert but it'll be same for all records inserted, no matter how many different records forms I submit. Other data like "message-to" and "message-by" insert fine. Here is the full page setup. Also, session_start(); is in the init.php <?php require_once 'core/init.php'; $user = new User(); $mainUserid = escape($user->data()->user_id); ?> <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <meta name="description" content="<?php echo $description; ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="css/style.css" media="screen"> </head> <body> $getRecord = $db->prepare("SELECT records.*, users.* FROM records LEFT JOIN users ON records.user_id = users.user_id ORDER BY record_id DESC LIMIT 10"); $getRecord->execute(); $resultRecord = $getRecord->fetchAll(PDO::FETCH_ASSOC); if(count($resultRecord) < 1) { $error = '0 records found.'; } else { foreach($resultRecord as $row) { $record_id = escape(intval($row['record_id'])); $user_id = escape(intval($row['user_id'])); $username = escape($row['full_name']); $title = escape($row['title']); $details = escape($row['details']); $_SESSION['record-id'] = $record_id; $_SESSION['message-to'] = $user_id; $_SESSION['message-by'] = $mainUserid; $message = Input::get('message'); // HTTP method POST or GET $_SESSION['message'] = $message; ?> <div class="record"> <h1><?php echo $title; ?></h1> <p><?php echo $details; ?></p> <p><?php echo $user_id; ?></p> <button class="button">send message</button> <!-- this form loads through on click(.button) function and is located in "ajax/form.php" --> <form class="new-form"> <textarea name="message" class="message">Enter message</textarea> <input type="button" name="submit" class="form-submit" value="Send Message"> </form> </div> <?php } } <!-- js scripts below --> <script src="js/jquery-1.11.0.min.js"></script> <script> $(document).ready(function(){ $(".button").click(function(){ $(this).parent().load("ajax/form.php"); }); }); </script> <script> $(document).on('click','.form-submit',function() { if($(".message").val()==='') { alert("Please write a message."); return false; } $(".form-submit").hide(); //hide submit button var myData = 'message='+ $(".message").val(); //build a post data structure jQuery.ajax({ type: "POST", // HTTP method POST or GET url: "process.php", //Where to make Ajax calls dataType:"text", // Data type, HTML, json etc. data:myData, //Form variables success:function(response){ $(".message").val(''); //empty text field on successful $(".form-submit").show(); //show submit button }, error:function (xhr, ajaxOptions, thrownError){ $(".form-submit").show(); //show submit button alert(thrownError); } }); }); </script> </body> </html> here is process.php <?php require_once 'core/init.php'; $session_record_id = $_SESSION['record-id']; $session_message_to = $_SESSION['message-to']; $session_message_by = $_SESSION['message-by']; $session_message = $_SESSION['message']; $insertMssg = $db->prepare("INSERT INTO sub_records(record_id, message_to, message_by, message, bid_posted) VALUES(:record_id, :message_to, :message_by, :message)"); $insertMssg->bindParam(':record_id', $session_record_id); $insertMssg->bindParam(':message_to', $session_message_to); $insertMssg->bindParam(':message_by', $session_message_by); $insertMssg->bindParam(':message', $session_message); $resultInsert = $insertMssg->execute(); if($resultInsert == false) { $error = 'Your message could not be inserted.'; } else { $success = 'Your message has been added.'; } ?> <div class="success"><?php echo $success; ?></div> <div class="error"><?php echo $error; ?></div>
  4. First thing. A correction in process.php. I am actually inserting the query into "sub_records" and not "records". As per your questions. 1. Yes I do have raw php in my html. It has never been an issue for me before and it does not give me errors. 2. Yes I am trying to pass the value of the .message form field via AJAX. Normally, the $_SESSION will show the value if I using a regular PHP method to submit the form. But since I am using AJAX to submit, i am getting an empty value from the $_SESSION. 3. Yes the session_start() is in more init.php. And no, I do not get any errors. I will make this a new topic.
  5. Sorry to bother you guys again but I just noticed something. Everything I submit the message to the database, it will not insert the "message" text(appears empty) and the "record_id" will be same for all records inserted, no matter how many different records I submit. Other data like "message-to" and "message-by" insert fine. Here is the full page setup. <?php require_once 'core/init.php'; $user = new User(); $mainUserid = escape($user->data()->user_id); ?> <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title><?php echo $title; ?></title> <meta name="description" content="<?php echo $description; ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" href="css/style.css" media="screen"> </head> <body> $getRecord = $db->prepare("SELECT records.*, users.* FROM records LEFT JOIN users ON records.user_id = users.user_id ORDER BY record_id DESC LIMIT 10"); $getRecord->execute(); $resultRecord = $getRecord->fetchAll(PDO::FETCH_ASSOC); if(count($resultRecord) < 1) { $error = '0 records found.'; } else { foreach($resultRecord as $row) { $record_id = escape(intval($row['record_id'])); $user_id = escape(intval($row['user_id'])); $username = escape($row['full_name']); $title = escape($row['title']); $details = escape($row['details']); $_SESSION['record-id'] = $record_id; $_SESSION['message-to'] = $user_id; $_SESSION['message-by'] = $mainUserid; $message = Input::get('message'); // HTTP method POST or GET $_SESSION['message'] = $message; ?> <div class="record"> <h1><?php echo $title; ?></h1> <p><?php echo $details; ?></p> <p><?php echo $user_id; ?></p> <button class="button">send message</button> <!-- this form loads through on click(.button) function --> <form class="new-form"> <textarea name="message" class="message">Enter message</textarea> <input type="button" name="submit" class="form-submit" value="Send Message"> </form> </div> <?php } } <!-- js scripts below --> <script src="js/jquery-1.11.0.min.js"></script> <script> $(document).ready(function(){ $(".button").click(function(){ $(this).parent().load("ajax/bid-form.php"); }); }); </script> <script> $(document).on('click','.form-submit',function() { if($(".message").val()==='') { alert("Please write a message."); return false; } $(".form-submit").hide(); //hide submit button var myData = 'message='+ $(".message").val(); //build a post data structure jQuery.ajax({ type: "POST", // HTTP method POST or GET url: "process.php", //Where to make Ajax calls dataType:"text", // Data type, HTML, json etc. data:myData, //Form variables success:function(response){ $(".message").val(''); //empty text field on successful $(".form-submit").show(); //show submit button }, error:function (xhr, ajaxOptions, thrownError){ $(".form-submit").show(); //show submit button alert(thrownError); } }); }); </script> </body> </html> And this is my process.php page. <?php require_once 'core/init.php'; $session_record_id = $_SESSION['record-id']; $session_message_to = $_SESSION['message-to']; $session_message_by = $_SESSION['message-by']; $session_message = $_SESSION['message']; $insertMssg = $db->prepare("INSERT INTO records(record_id, message_to, message_by, message, bid_posted) VALUES(:record_id, :message_to, :message_by, :message)"); $insertMssg->bindParam(':record_id', $session_record_id); $insertMssg->bindParam(':message_to', $session_message_to); $insertMssg->bindParam(':message_by', $session_message_by); $insertMssg->bindParam(':message', $session_message); $resultInsert = $insertMssg->execute(); if($resultInsert == false) { $error = 'Your message could not be inserted.'; } else { $success = 'Your message has been added.'; } ?> <div class="success"><?php echo $success; ?></div> <div class="error"><?php echo $error; ?></div>
  6. It seems like the reason it's not submitting the form is because the form is being loaded through ajx/jquery. If I just have the form directly on the page, it does process. But I need to have it only show when clicking the button. So how can I do both?
  7. I seem to have a new issue. It seems after I load the form and submit the message, it won't work with jquery/ajax. Here is my jquery code to send a message. $(document).ready(function() { $(".form-submit").click(function (e) { e.preventDefault(); if($(".message").val()==='') { alert("Please write a message."); return false; } $(".form-submit").hide(); //hide submit button var myData = 'message='+ $(".message").val(); //build a post data structure jQuery.ajax({ type: "POST", // HTTP method POST url: "process.php", //Where to make Ajax calls dataType:"text", // Data type, HTML, json etc. data:myData, //Form variables success:function(response){ $(".message").val(''); //empty text field on successful $(".form-submit").show(); //show submit button }, error:function (xhr, ajaxOptions, thrownError){ $(".form-submit").show(); //show submit button alert(thrownError); } }); }); }); It's simply not making the connection. I even tried a simple alert and still no go when I press submit. <script> $(document).ready(function() { $(".form-submit").click(function (e) { alert('hello there'); }); }); </script>
  8. Perfect, that works like a a charm. As per ".new-form", i am loading this div class from form.php page. Do you have a better method for it?
  9. To make it more clear, when I press the ".button", it will load the "form" in "#record" container. Like this. <div class="record"> <h1>Record title 3</h1> <p>Record description</p> <button class="button">send message</button> <form class="new-form"> <textarea name="message" class="message"></textarea> <input type="button" name="submit" class="form-submit" value="Send Message"> </form> </div> From that point on, I will be using ajax to submit that message. But again the issue is that the form loads in ALL the ".record" containers at the same time when I press only ONE ".button". I just like to make it so that it's unique to each container.
  10. For eg, say I am retreiving 3 records from database and they look like this. <div class="record"> <h1>Record title 1</h1> <p>Record description</p> <button class="button">send message</button> </div> <div class="record"> <h1>Record title 2</h1> <p>Record description</p> <button class="button">send message</button> </div> <div class="record"> <h1>Record title 3</h1> <p>Record description</p> <button class="button">send message</button> </div> and this is my ajax to load the form. <script> $(document).ready(function(){ $(".button").click(function(){ $(".record").load("form.php .new-form"); }); }); </script> the form looks like this. <form class="new-form"> <textarea name="message" class="message"></textarea> <input type="button" name="submit" class="form-submit" value="Send Message"> </form> That all works. The only problem with that is, it loads the form in EACH record. How can I make it so that the form only loads to the div record from which I am calling from?
  11. my .htaccess has exactly what you have, except for the obvious change from test to record.ph. I have followed your instruction 100% and still no go. I think it leaves to point out that something might be amiss within my xampp setup. I may have to reinstall it or test it on a live server. Thank you so much for your help though. Even though it did not work out for me, I still learned a bit more Rewrite rules.
  12. I added the above exactly as it is. And I assume the link is still ike this. <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a> Still shows the "object not found" error. Are you sure the link is supposed to be set up that way because it comes out as http://localhost/id/8/name/fashion? I just noticed that the page name is missing before the id. Though if I do have it like this(without .php extention), it will give me an "internal server error". <a href="test/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a> And if I set it with .php extention, it will show the page, but again without the $_GET parameters. <a href="test.php/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link here</a>
  13. Actually .htaccess is being read. I know this because I have another line in there that removes the .php extention from the pages and it does show the pages without the .php. RewriteRule ^(.*)$ $1.php [L] I have Apache installed through XAMMP.
  14. I have a register form where a user enters a full name, email and password. I do not want to give them the option of choosing their own "username"; rather I want to automatically create the username from their "full name". So far I can create the username by removing space and symbols from full name. Like this. $fullname = trim($_GET['fullname']); $username = preg_replace('/\s+/', '', $fullname); Looking at that, John Smith would become "johnsmith". Since there could be dozens of people with the name "john smith", I was thinking of assigning unique number to each of them. Like this "johnsmith1", "johnsmith2", "johnsmith3"..etc. How would you say I go on incrementing the username like that?
  15. I copied and pasted your code exactly as shown above. I still get the object not found error. My .htaccess and record.php are both in the main directory. But the html "link" is in a different folder like this. "main directory/template/link.php". Could this have something to do with it?
  16. Here is update. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/$ record.php?id=$1&name=$2 [L,QSA] <a href="/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> Doing all that, now I get an error like this on record.php. And the url appears like this. http://localhost/id/2/name/category-name-here Object not found! The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
  17. Alright so just to go over, here is the updated code. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^id/([0-9]+)/name/([a-zA-Z-]+)/$ record.php?id=$1&name=$1 [L,QSA] Link to record page <a href="record.php/id/23/name/category-name-here">Link to record page</a> Having done all this, I am still getting "0" strings for the get paremeters(id, name) on record.php page.
  18. Ah yes that makes more sense. It's still not working though. I think it maybe because I have a slug-name for a name. For eg. <a href="record.php/id/23/name/category-name-here">Link to record page</a> So how would you take the hyphen(-) into consideration in your above rewrite rule?
  19. Here is my .htaccess. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^([0-9]+)/([a-zA-Z]+)/$ record.php?id=$1&name=$1 [L,QSA] My link to record page. <a href="record.php/id/<?php echo $id; ?>/name/<?php echo $name; ?>">Link to record page</a> On record.php page, I tried to get the GET parameters like this. The var dump returns string(0) "" string(0) "" . $id = $_GET['id']; $name = $_GET['name']; var_dump($id); var_dump($name); The record page does show up, but it's messed up because it uses the above GET parameters to retrive data from database. So I was wondering if you can tell me what am I doing wrong and how can I fix it?
  20. I would like to know how can I redirect a jquery dialog function to a different page, if another function is true? For eg. 1. User 1 sends a request to User 2. 2. User 2 accepts the request. 3. User 1 still has the request "dialog" window open. Instead, I would like to have that dialog window close and redirect to a specified page. Of course this only happens after User 2 accepts the request and I check against it in the database. Here is the function for when User 1 makes a request. <script> $(function() { $("#new-dialog").dialog({ resizable: true, autoOpen: false, modal: true, width: 600, height: 300, buttons: { "Cancel Trade": function(e) { $(this).dialog("close"); $.ajax({ type:"post", url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>", data:"action=cancelTrade", success:function(data){ if(data == true) { alert('success'); } else { alert('not deleted'); } //window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>'; } }); } } }); $(".dialogify").on("click", function(e) { e.preventDefault(); $("#new-dialog").html(""); $("#new-dialog").dialog("option", "title", "Loading...").dialog("open"); $("#new-dialog").load(this.href, function() { $(this).dialog("option", "title", $(this).find("h1").text()); $(this).find("h1").remove(); }); }); }); </script> And here is the new function in which I check the database for match. If it returns true, I would like to redirect the above dialog/page. <script> $('document').ready(function(){ function checkTrade() { $.ajax({ type:"post", url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>", data:"action=checkTrade", success:function(data){ window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>'; } }); } }); </script> So what am I missing to do the redirect?
  21. Just tried switching browsers. Definitly weird. I will have to go back and recheck the queries and match them correctly to relative user.
  22. Actually I just noticed something else. Since I am doing user to user request, I have 2 browsers open. Firefox and Chrome. I am logged into 2 different accounts. Well I just noticed that on firefox account(User 1), it displays both session variables correctly. But on Chrome(User 2), it only displays 1 variable correctly(which is $session_requestToId).
  23. Here I have it layed out on a page to give you a better look. I should also mention that it won't delete(using action=cancelTrade) my database row when I cancel my trade session as User 2. But it does delete the row when I cancel it as User 1. <?php require_once 'core/init.php'; $user = new User(); $myUserid = $user->data()->user_id; // this is my user_id as a logged in user $session_requestById = $_SESSION['requestById']; $session_requestToId = $_SESSION['requestToId']; ?> <!DOCTYPE HTML> <html lang="en"> <head> <script src="js/jquery-1.11.0.min.js"></script> <script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"> <script> // this script when User 1 makes a trade request. $(function() { $("#new-dialog").dialog({ resizable: true, autoOpen: false, modal: true, width: 600, height: 300, buttons: { "Cancel Trade": function(e) { $(this).dialog("close"); $.ajax({ type:"post", url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>", data:"action=cancelTrade", success:function(data){ } }); } } }); $(".dialogify").on("click", function(e) { e.preventDefault(); $("#new-dialog").html(""); $("#new-dialog").dialog("option", "title", "Loading...").dialog("open"); $("#new-dialog").load(this.href, function() { $(this).dialog("option", "title", $(this).find("h1").text()); $(this).find("h1").remove(); }); }); }); </script> <script> // this script when User 2 accepts the trade request. $(function() { $( "#dialog-confirm" ).dialog({ resizable: true, autoOpen: true, modal: true, width: 600, height: 300, buttons: { "Accept Trade": function() { $( this ).dialog( "close" ); $.ajax({ type:"post", url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>", data:"action=acceptTrade", success:function(data){ window.location.href='trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>'; } }); }, Cancel: function() { $( this ).dialog( "close" ); $.ajax({ type:"post", url:"request-trade?by=<?php echo $session_requestById; ?>&to=<?php echo $session_requestToId; ?>", data:"action=cancelTrade", success:function(data){ if(data == false) { alert('not cancelled'); } else { alert('cancelled success'); } } }); } } }); }); </script> </head> // assume the content in the body below is trade.php <body> <?php //query here if(count($result) > 0) { foreach($result as $row) { $user_id = intval($row['user_id']); } $_SESSION['requestById'] = $myUserid; $_SESSION['requestToId'] = $user_id; } ?> </body> </html> And here is my request-trade.php <?php require_once 'core/init.php'; $user = new User(); $mainUserid = intval($user->data()->user_id); $session_requestById = intval(Input::get('by')); $session_requestToId = intval(Input::get('to')); $action = $_POST['action']; if($action == "cancelTrade") { try { $delete = $db->prepare("DELETE FROM trade_session WHERE request_by = :request_by AND request_to = :request_to AND session >= :session"); $delete->bindParam(':request_by', $session_requestById); $delete->bindParam(':request_to', $session_requestToId); $delete->bindValue(':session', 1); $delete->execute(); } catch(Exception $e) { die($e->getMessage()); } } else if($action == "acceptTrade") { try { $stmt = $db->prepare("UPDATE trade_session SET session = :session WHERE request_to = :request_to"); $stmt->bindValue(':session', 2); $stmt->bindParam(':request_to', $session_requestToId); $stmt->execute(); } catch(Exception $e) { die($e->getMessage()); } } else if($action == "requestTrade") { try { $find = $db->prepare("SELECT request_by, request_to, session FROM trade_session WHERE request_by = :request_by AND request_to = :request_to AND session >= :session"); $find->bindParam(':request_by', $session_requestById); $find->bindParam(':request_to', $session_requestToId); $find->bindValue(':session', 1); $find->execute(); $resultFind = $find->fetchAll(PDO::FETCH_ASSOC); if(count($resultFind) > 0) { echo 'Your request is already sent.'; } else { echo 'You have sent trade request to ' . $session_requestToId; $session_date = date('Y-m-d H:i:s'); $stmt = $db->prepare("INSERT INTO trade_session(request_by, request_to, session, session_date) VALUES(:request_by, :request_to, :session, :session_date)"); $stmt->bindParam(':request_by', $session_requestById); $stmt->bindParam(':request_to', $session_requestToId); $stmt->bindValue(':session', 1); $stmt->bindParam(':session_date', $session_date); $stmt->execute(); } } catch(Exception $e) { die($e->getMessage()); } } else { // hide }
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.