Jump to content

alan_k

Members
  • Posts

    45
  • Joined

  • Last visited

Profile Information

  • Location
    New York
  • Interests
    LAMP, Web Development

alan_k's Achievements

Newbie

Newbie (1/7)

0

Reputation

  1. Yes you are right. Never do this on a production site. I am just learning JS & php and did this as an exercise following "The Book of JavaScript, 2nd Edition: A Practical Guide to Interactive Web Pages". I was trying to add on to what author had left out which was a way to clear the todo list's completed items.
  2. Sorry for the delay in responding. I just want to thank every one for input and post how I solved issue. I will have to revisit using the $_POST array with AJAX cause I was never able to pass a variable thru it. Javascript: // Clear Done Items Functionfunction clear_done_items(){var xmlhttp;var temp = document.getElementById("list_section");var temp_list_name = temp.innerHTML;var str_len = temp_list_name.length;var list_name = temp_list_name.slice(17,str_len);var user_name = getNameFromCookie();if (user_name == list_name) { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }xmlhttp.open("POST","todolist/clear_done_items.php",true);xmlhttp.send();} else { alert("Warning: You Must be Logged in as this User, " + list_name + ", to Delete the Completed Items List"); }} and php: <!DOCTYPE html><html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"><title>Clear Done Items</title></head><body><?phpini_set('display_errors',1);error_reporting(E_ALL);$xml_cookie = $_COOKIE['user'];$xml_file_name = $xml_cookie . ".xml"; /* Here we will use the The DOMDocument class functions to delete the text nodes.*/ //Format XML to save indented tree rather than one line$domxml = new DOMDocument('1.0');if ($domxml->load($xml_file_name) === TRUE) {$domxml->preserveWhiteSpace = false;$domxml->formatOutput = true;// Start Delete process of nodeecho "<br>";$xpath = new DOMXPath($domxml);// Here we use the The DOMXPath class function evaluate to do the heavy lifting.foreach ($xpath->evaluate('//doneitems/item') as $node) { $node->parentNode->removeChild($node);}//Save XML to file - remove this and following line if save not desired$domxml->save($xml_file_name);echo "<strong>XML Document updated.</strong>"; } else {echo " <strong>Error in loading XML file:</strong>"; echo "<br>"; print_r(error_get_last()); } ?></body></html> so basically I'm using a cookie to get the user_name variable to php to process from the main page.
  3. Here is function: function displayHomeInformation(user_name) { document.getElementById("loginArea").innerHTML = "<h3>Welcome, " + user_name + ". <br>" + "<a href='#' onClick='logout(); return false'>logout</a> </h3>"; displayLegalLists(user_name);} It just displays some login information for user. But forget about that I cant even get the POST array to my php page. I'm lost as to why this happening. I've verified you dont need a form to submit and create the POST array, yes? These 3 lines should create and send the POST array to my php file: xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(user_name); And yet the array is empty??? Is my understanding wrong? What is responsible for creating and sending the POST array? And what is it I'm doing wrong. So to be clear step 1 I get user_name from cookie and put it in a variable step-2 I issue the xmlhttp commands mentioned above that will create the POST array and send my user_name variable in the array to my php file step -3 The php file looks for the user_name in the POST array and assigns it to php variable and then I can work on the appropriate XML file based on the user_name. Thanks for your time.
  4. That's what I thought by reading up on ajax, that all I needed was the xmlhttp.send. Any ideas then on why the _POST array is empty when I send it to my php file? I even tried GET and that didnt work. I'm really stumped by what I'm doing wrong here. Any input much appreciated...
  5. I'm sorry it does or it doesnt have to be submitted in form? I know i'm not getting to php file because print_r($POST) is printing out array ().
  6. I changed line to xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send('user_name='+ user_name); but get same exact error. $_POST array is still empty. Just to be clear I dont need the ".onreadystatechange" event handler if all I want is to pass the user_name to the php script and dont need anything back form the script?
  7. I am trying to pass a variable from a javascript function to my php file. All I do is extract the user_name from a cookie in javascript and then use AJAX to $_POST the variable to my php file for processing. I do not use a form to submit the POST variable which I have done in the past.The problem is my $_POST array is not getting passed to the php file. I've looked here and looked at several other suggestions but none work. Do you have to submit the $_POST variable via an html form? I dont think so but maybe I'm wrong. Here is the code:Javascript - function clearDoneItems() { var user_name = getNameFromCookie(); if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } alert(user_name); xmlhttp.open("POST","todolist/clear_done_items.php",true); xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xmlhttp.send(user_name); displayHomeInformation(user_name); } PHP - <!DOCTYPE html> <html> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> <title>Clear Done Items</title> </head> <body> <?php ini_set('display_errors',1); error_reporting(E_ALL); print_r($_POST); $xml_name=$_POST['user_name']; $xml_file = $xml_name . ".xml"; echo $xml_file; /* Here we will use the The DOMDocument class functions to delete the text nodes. Format XML to save indented tree rather than one line */ $domxml = new DOMDocument('1.0'); if ($domxml->load('$xml_file') === TRUE) { $domxml->preserveWhiteSpace = false; $domxml->formatOutput = true; // Start Delete process of node echo "<br>"; $xpath = new DOMXPath($domxml); // Here we use the The DOMXPath class function evaluate to do the heavy lifting. foreach ($xpath->evaluate('//doneitems/item') as $node) { $node->parentNode->removeChild($node); } //Save XML to file - remove this and following line if save not desired $domxml->save('alan.xml'); echo "<strong>XML Document updated.</strong>"; } else { echo " <strong>Error in loading XML file:</strong>"; echo "<br>"; print_r(error_get_last()); } ?> </body> </html> >Errors on php page:Notice: Undefined index: user_name in /var/www/bb/todolist/clear_done_items.php on line 160Warning: DOMDocument::load(): I/O warning : failed to load external entity "/var/www/bb/todolist/$xml_file" in /var/www/bb/todolist/clear_done_items.php on line 24 I thought from reading up on AJAX the POST variable is set from the three commands xmlhttp.open,.setRequestHeader and .send. Dont these take the place of the POST action in an html form?
  8. Just wanted to share the code in case some other newbie ever needs it. This stores the $id variable then calls the modal box to delete the record. Part of my trouble was I had to read up on scopes. Declaring id as var id was making it a local variable, only accessible within that function. Removing the var part makes the id variable default to a Global variable and thus accessible outside the scope of the function. Anyhow here it is and please feel free to weigh in to improve the solution. JS: <!--JavaScript for popup alert to Delete a row using Bootstrap modal --> <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '.delete-row', function(e){ e.preventDefault(); id = $(this).attr('data-id'); // get id from td element and store it in Global variable $id. }); $(document).on('click', '#del-row', function(e){ window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script>
  9. Thanks I finally got it to work as follows: js: <!--JavaScript for popup alert to Delete arow using Bootstrap modal--> <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '.delete-row', function(e){ e.preventDefault(); var id = $(this).attr('data-id'); // get id from td element and store it in $id. window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script> PHP: echo "<td><a class='one glyphicon glyphicon-trash delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; Now if you still have any patience left, that deletes the row fine the only thing is I would like the Bootstrap modal delete confirmation button to do the actual delete rather then the trashcan icon in the table. If you have any idea on how to proceed on that : Bootstrap code: <!-- Modal Code --><div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> <h4 class="modal-title" id="myModalLabel">Delete Row Confirmation</h4> </div> <div class="modal-body"> Are You Sure You Want To Delete This Row? </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" id="del-row" class="btn btn-primary">Delete Row</button> </div> </div> </div></div> Thanks again for your patience.
  10. I'm sorry I'm getting a little punch drunk with this one. The original code segment is <!--JavaScript for popup alert to Delete arow using Bootstrap modal <script type="text/javascript"> $(document).ready(function(){ $(document).on('click', '#del-row', function(e){ e.preventDefault(); var id = $("#delete-row").attr('data-id'); // get id from td element and store it in $id. window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row. }); }); </script> --> And the poster who I got this from advised that the data-id value would have the current id from the row : echo "<td><a class='one glyphicon glyphicon-trash delete-row' id='delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; We now know that is wrong. Is there a correct way to assign the id var the current clicked id value so I can assign it and use it in the window.location command? Or perhaps you have a more elegant way? Thanks.
  11. OK when I did it like this: <script type="text/javascript"> $(document).ready(function(){ $( "#del-row" ).click(function() { $( this).remove();});}); </script> That made the Delete Row on the modal disappear:( When I did this : <script type="text/javascript"> $(document).ready(function(){ $( "#del-row" ).click(function() { $( delete-row).remove();});}); </script> Modal box does nothing when I click on delete row button. I'm confused to because I dont want to delete the row on this page but I want to get the $row[id} value so I can call the "window.location = 'printer_delete.php?id=' + id;" line and send it. Here is a link to pastebin with the code for the page: http://pastebin.com/pFR5itswmaybe this will help to see what I'm trying to accomplish.Thanks for yopur patience...
  12. I tell you I tried like a dozen different examples and none worked. If someone could help me out here and show me the jquery I need to get this to work I would be extremely grateful.Thanx...
  13. I've been modifying my site and the script is supposed to get the id value stored in $row[id] field and then call delete_printer.php?id. This logic works fine for me modifying a record yet when doing a delete I get only the first row id for all rows on that page I try to delete. I've used this same line many times with no trouble and yet now it is broken and I cant see why. JS: <script type="text/javascript">$(document).ready(function(){$(document).on('click', '#del-row', function(e){e.preventDefault();var id = $("#delete-row").attr('data-id'); // get id from td element and store it in $id.window.location = 'printer_delete.php?id=' + id; // call printer_delete.php with $id value to delete row.});});</script> PHP: echo "<td><a class='one glyphicon glyphicon-edit' aria-hidden='true' data-toggle='tooltip' data-placement='bottom' title='Click Here to Edit' href='printer_update.php?id=" . $row['id'] . "'></a></td>";echo "<td><a class='one glyphicon glyphicon-trash delete-row' id='delete-row' data-toggle='modal' data-placement='bottom' title='Delete Printer Record' href='#' data-target='#myModal' aria-hidden='true' data-id='" . $row['id'] . "'></a></td>"; The first PHP line to modify a record works fine. This is the first time I've used the PHP in conjunction with the Javascript so I'm thinking in there lies the problem but being relatively new to JS I'm in need of some help. Any input would be welcome.
  14. This was the trouble: Black_Toner_Cart_Num = :Black_Toner_Cart_Num Table was Blk_Toner_Cart_Num. Is there anyway to find these MySQL error messages like you can with PHP?
  15. I have a page that adds printer info into an Inventory tracking site. It passes form values to insert_printer.php via _$POST. I have used the following code before to insert a row with no problems and yet now it is not working. I have verified the php with a validator and that is correct. I put in a a print_r ($_POST) and the values are being passed to the insert page ok. I have looked at this thing for hours and nothing is popping out. Perhaps a new set of eyes will see it right away: $stmt = $link->prepare("INSERT INTO Printer_Inventory SET Printer_Name = :Printer_Name, Physical_Loc = :Physical_Loc, Static_IP_Addr = :Static_IP_Addr,Printer_Model = :Printer_Model, Black_Toner_Cart_Num = :Black_Toner_Cart_Num, Blk_Toner_Changed = :Blk_Toner_Changed,Cyan_Toner_Cart_Num = :Cyan_Toner_Cart_Num, Cyan_Toner_Changed = :Cyan_Toner_Changed,Mgnt_Toner_Cart_Num = :Mgnt_Toner_Cart_Num, Mgnt_Toner_Changed = :Mgnt_Toner_Changed,Yllw_Toner_Cart_Num = :Yllw_Toner_Cart_Num, Yllw_Toner_Changed = :Yllw_Toner_Changed");$stmt->execute(array(':Printer_Name' => $Printer_Name,':Physical_Loc' => $Physical_Loc,':Static_IP_Addr' => $Static_IP_Addr,':Printer_Model' => $Printer_Model,':Black_Toner_Cart_Num' => $Black_Toner_Cart_Num,':Blk_Toner_Changed' => $Blk_Toner_Changed,':Cyan_Toner_Cart_Num' => $Cyan_Toner_Cart_Num,':Cyan_Toner_Changed' => $Cyan_Toner_Changed,':Mgnt_Toner_Cart_Num' => $Mgnt_Toner_Cart_Num,':Mgnt_Toner_Changed' => $Mgnt_Toner_Changed,':Yllw_Toner_Cart_Num' => $Yllw_Toner_Cart_Num,':Yllw_Toner_Changed' => $Yllw_Toner_Changed)); This works ok manually from phpMyAdmin: INSERT INTO Printer_Inventory (`Printer_Name`,`Physical_Loc`,`Static_IP_Addr`,`Printer_Model`) VALUES ('HP','SHS','172.16.xxx.xxx','HP 3102'); Get an error message: I am correct in assuming I dont have to account for the primary key id field? I never had to before. I imported a .csv file to the database ok. Attached is the SQL structure. Printer_Inventory.txt
×
×
  • Create New...