ngreenwood6 Posted October 3, 2008 Share Posted October 3, 2008 I am new to ajax so I have a quick question for someone that works with it quite often. I know that with php it is better to use post when submitting userdata that you dont want anyone to see because it is posted in the url. However, when I use the get command in ajax it doesnt post anything in the url. So would it be ok to use GET to use the userdata that you dont want anyone to see or is it better to use the POST. Thanks for any help Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/ Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Use POST. If you use GET, all of the data is listed in the URL. That is the difference between POST and GET. So, if you want it to be hidden, you use POST. Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656345 Share on other sites More sharing options...
ngreenwood6 Posted October 3, 2008 Author Share Posted October 3, 2008 Ok thank you for the response. Can you show me an example of how to use the post then because I can use the get but I am not quite sure how to use the post. I am in the process of reading a book and it shows good examples of the get but not the post which is weird because all of the php books show the post and a little get. Anyways looked on google for a good example but they want to make everything complex. Any help is appreciated once again F1Fan!! Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656355 Share on other sites More sharing options...
Maq Posted October 3, 2008 Share Posted October 3, 2008 There's a million online, but here's a good one: Example of $_POST Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656357 Share on other sites More sharing options...
ngreenwood6 Posted October 3, 2008 Author Share Posted October 3, 2008 I know how to do a php post. That is why I am in the AJAX forum. I am looking for how to submit the request to the php page in AJAX. Also would the php page then use post or get? Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656360 Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Posting data to PHP works really well, but it also requires a page to be reloaded. Using AJAX is for submitting and receiving PHP data WITHOUT reloading a page. So really, using AJAX you're not using a form with traditional GET or POST. It works more like the GET method, but not exactly. List your code and tell us more of what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656362 Share on other sites More sharing options...
Maq Posted October 3, 2008 Share Posted October 3, 2008 I know how to do a php post. That is why I am in the AJAX forum. I am looking for how to submit the request to the php page in AJAX. Also would the php page then use post or get? Sorry, I think I'm brain dead today... Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656368 Share on other sites More sharing options...
ngreenwood6 Posted October 3, 2008 Author Share Posted October 3, 2008 Say I had this function function submitUser(str) { var url="get.php"; url=url+"?username="+str; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true) xmlHttp.send(null) then i had this php page <?php //database variables $host = "localhost"; $user = "user"; $pass = "pass"; $db = "database"; //get from the form $username = $_GET['username']; //connect to database mysql_connect($localhost,$user,$pass); //select database mysql_select_db($db); //query $query = "INSERT INTO table (username) VALUES ('$username')"; //insert the results $result = mysql_query($query); ?> That would take what the user inputs into the form and submit it into the database but like you said it is passed throught the url which is fine for this example but if I added the password it would not be good. How would I do the post for this example in the function and then how would the php page change? Another question I have is how do you pass more than one variable (for example adding the password the the function so that I can call this onclick of the submit button)? Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656379 Share on other sites More sharing options...
KevinM1 Posted October 3, 2008 Share Posted October 3, 2008 Use POST. If you use GET, all of the data is listed in the URL. That is the difference between POST and GET. So, if you want it to be hidden, you use POST. I believe there's also a size limitation to GET as well. So, if the OP is transferring a lot of data, POST is definitely their best bet. I know how to do a php post. That is why I am in the AJAX forum. I am looking for how to submit the request to the php page in AJAX. Also would the php page then use post or get? Well, you'd build your query string in the same way, so say you had a username and password: var userName = encodeURIComponent("Billy Idol"); var password = encodeURIComponent("12345fuy8ytt5"); var parameters = "user=" + userName + "&pass=" + password; xmlHttp.open("POST", "yourscript.php", true); xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //not sure if it's needed, but it works xmlHttp.onreadystatechange = handleRequest; xmlHttp.send(parameters); For the PHP page, all you'd need to do is handle the POST data: $user = $_POST['user']; $password = $_POST['pass']; Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656384 Share on other sites More sharing options...
F1Fan Posted October 3, 2008 Share Posted October 3, 2008 Well, personally I don't think it's a good idea to use AJAX for a log in process. But, if you really want to, it's slightly more secure than the basic GET method, because the user won't actually SEE the username and password being sent. As far as passing multiple variables, just do this: function submitUser(str,pwd) { var url="get.php"; url=url+"?username="+str; url=url+"?password="+pwd; url=url+"&sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true) xmlHttp.send(null) } Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656385 Share on other sites More sharing options...
ngreenwood6 Posted October 3, 2008 Author Share Posted October 3, 2008 Thanks that is exactly what I was looking for. Nightslyr and F1Fan you guys are great. I dont understand why you cant just find simple tutorials with this information in it. In my opinion some of the people making tutorials assume that you have a good knowledge of php or ajax for this matter and use complex codes to show a tutorial. It would be nice if there was some tutorials out there that just showed basic commands and even explained each part of it. Maybe I will come up with something like that. Anyways thanks to the both of you and if you guys are ever in need of anything please let me know and I will be more than willing to help. EDIT: Oh yeah I meant to tell you too F1Fan that I am not using it for the log in I was just using that as a simple example lol. Quote Link to comment https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656432 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.