Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/
Share on other sites

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!!

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656355
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656362
Share on other sites

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)?

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656379
Share on other sites

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'];

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656384
Share on other sites

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)
}

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656385
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/126897-solved-get-and-post/#findComment-656432
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.