laponac84 Posted January 12, 2013 Share Posted January 12, 2013 Hi, How to get: 1. Part of URL with jquery for example: ..index.php?program_id=1&option=add i wont to get valuer program_id = ? and option= ? 2. How to get value of php session? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/ Share on other sites More sharing options...
Jessica Posted January 12, 2013 Share Posted January 12, 2013 1. I don't believe Javascript can access the query string part of a URL. 2. What? Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405241 Share on other sites More sharing options...
MDCode Posted January 13, 2013 Share Posted January 13, 2013 There are only a few things I can think of to even get the URL in JQuery, let alone get data from it. Perhaps you can somehow modify this: <!-- Full URL http://domain.com/file.whatever?anyvar=something --> <script type="text/javascript"> alert($(location).attr('href')); </script> Your second question however makes no sense. I assume you want to access php sessions in your JQuery? In which case it should work just putting in the source code. <?php session_start(); $_SESSION['uid'] = "23123"; ?> <!-- other stuff --> <script type="text/javascript"> <?php echo "var stuff= '".$_SESSION['uid']. "';"; ?> alert('Err...hi? '+stuff+' '); </script> Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405281 Share on other sites More sharing options...
laponac84 Posted January 13, 2013 Author Share Posted January 13, 2013 Situation: i have 2 mysql table`s 1. tbl_document (id, number_of_document) 2. tbl_items_of_document (id, name_of_item, document_id, user_id) now, clasic php html form <form action="insert.php?document_id=<?php echo $id; ?>" method="post"> ..... </form> after form we use $document_id = $_GET["document_id"]; $name = $_POST["name_of_item"]; //... mysql_query("INSERT INTO `tbl_items_of_document` SET `name_of_item` = '$name', `document_id` = '$document_id', `user_id` = '$user_id'")or die(mysql_error()); we have all necessary data inserted in the table And the we come to problem if we want to use jquery with code below, i can insert data in one independet mysql table jQuery Basics: POST/GET Data (HTTP Request) index.php [/size][/font][/color] <input type="text" id="name" /> <div id="name_feedback"></div> [color=#000000][font=Tahoma, Verdana, Arial][size=3] jfunc.js [/size][/font][/color] $('#name').keyup(function(){ var name = $('#name').val(); $.post('proces.php'), {name:name}, function(data) { $('#name_feedback').html(data); }); }); [color=#000000][font=Tahoma, Verdana, Arial][size=3] proces.php [/size][/font][/color] <?php if(isset($_POST['name'])){ $name = $_POST['name']; echo $name; //or mysql_query..... [color=#000000][font=Tahoma, Verdana, Arial][size=3] and this code is ok for insert into independent table what if we have relation database, with two tables? 1. tbl_document (id, number_of_document) 2. tbl_items_of_document (id, name_of_item, document_id, user_id) and i want to insert data into tbl_items_of_document, and i nead for that: - value of `id` from tbl_document - and value of `session` how to GET `id` of the document to write in to `tbl_items_of_document` with jQuery, or, how to GET session id with jquery, becose i want to know who create that item or item`s? thanks Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405312 Share on other sites More sharing options...
MDCode Posted January 13, 2013 Share Posted January 13, 2013 Everything on your proces.php will run. So, why not assign your session id into a variable on proces.php? $session_id = $_SESSION['id']; Of course you would also need a session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405341 Share on other sites More sharing options...
Adam Posted January 13, 2013 Share Posted January 13, 2013 FYI you can access the query string in Javascript using the window.location.search property. Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405393 Share on other sites More sharing options...
Jessica Posted January 13, 2013 Share Posted January 13, 2013 FYI you can access the query string in Javascript using the window.location.search property. My bad, I was thinking of the # part I think? Who knows. Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405395 Share on other sites More sharing options...
Adam Posted January 13, 2013 Share Posted January 13, 2013 My bad, I was thinking of the # part I think? Who knows. window.location.hash Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405398 Share on other sites More sharing options...
Jessica Posted January 13, 2013 Share Posted January 13, 2013 Hmmm now I'm wondering what it was that I couldn't use JS for that one time.... *wanders off* Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405404 Share on other sites More sharing options...
haku Posted January 14, 2013 Share Posted January 14, 2013 This function will do it for you: function getGet(name) { var regexS, regex, results; name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); regexS = "[\\?&]" + name + "=([^]*)"; regex = new RegExp(regexS); results = regex.exec(documentSrc); if(results !== null) { return decodeURIComponent(results[1].replace(/\+/g, " ")); } return false; } So if your URL is www.example.com?key=value, you can get $_GET['key'] using: getGet(key); Quote Link to comment https://forums.phpfreaks.com/topic/273076-jquery-get-part-of-url-and-session/#findComment-1405505 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.