Jump to content

GET(action)


jimfog
 Share

Recommended Posts

I know that get is a superglobal array and that is seen anywhere in the script. I know also(and tell me if I am wrong) that it is related with links...I mean you a pass a value to GET with a link. Recently though, I came across with a scenario where the input of a user in an input box was grabbed by a GET['action'] variable. Here is the HTML-it is a tutorial on AJAX:

<form name="autofillform" action="autocomplete.php">		    <table border="1" cellpadding="5">			    <thead>				    <tr>										    <th><strong>Composer Name:</strong></th>					    <th><input type="text"  size="40" id="complete-field"  onkeyup="doCompletion();"></th>				    </tr>			    </thead>			    <tbody>                   			    </tbody>		    </table>	    </form>

IN general do user inputs in an input box(search...as in this example here) get "grabbed" from GET['action']?

Link to comment
Share on other sites

I'm not sure what you mean by "grabbed." If by "links" you mean the address specified by the action field, then yes, GET is related to that -- but only if the form is submitted normally, so that the action gets a query string added. If you are using AJAX, name-value pairs must be added to the query string programmatically, and they don't have to be added at all. In your case, whatever happens, probably happens in the doCompletion function.

Link to comment
Share on other sites

I'm not sure what you mean by "grabbed."
Maybe I should have said "passed" to the GET variable.Here is the docomplete function that handles the ajax call:
function doCompletion() {	    var url = "autocomplete.php?action=complete&id=" + escape(completeField.value);	    req = initRequest();	    req.open("GET", url, true);	    req.onreadystatechange = callback;	    req.send(null);}

This is what I have come to understand so far:The letters entered in the input box by the user get passed to the function,and specifically to the url...and afterwards can the be accessed by a GET array. I am I saying it correctly?Here is the tutorial so you can have the whole picture....https://netbeans.org/kb/docs/php/ajax-quickstart.html#overview

Link to comment
Share on other sites

nothing is passed to the function, since it doesn't accept any parameters. The entire form is submitted via AJAX, the form is just a semantic wrapper it looks like. The function1) gets the value of the users input and constructs a url2) call initRequest (likely returns the correct XMLHttpRequest object)3) creates an ASYNC request that uses GET, and the url from #14) assigns callback to onreadystatechage (you need to find out where callback is defined, it should be a function that accepts the response of the request)5) sends the request

Edited by thescientist
Link to comment
Share on other sites

1) gets the value of the users input and constructs a url...
This is what I was expecting to hear and my main question was. I suppose completeField.value gets the user input.
Link to comment
Share on other sites

I suppose completeField.value gets the user input.
Yes. In the tutorial, this function assigns a reference to the user input to the global variable completeField. I see no reason to create a global variable in this way, but that's how it works.
function init()   completeField = document.getElementById("complete-field");}

Edited by Deirdre's Dad
Link to comment
Share on other sites

In the same tutorial there is a check if the user has filled in the input box:

if(isset($_GET['action']) && $_GET['action'] == "complete")

I do not understand the second part in the above line "$_GET['action'] == "complete"".I suppose it checks that the user has finished entering value in the input box.The question is where does this ''complete" comes from?Is it the one in the URL constructed in the doCompletion function?

Link to comment
Share on other sites

var url = "autocomplete.php?action=complete&id=" + escape(completeField.value); There it is.
Ok... :Bucktooth:Then...I do not understand...why he checks for isset($_GET['action']) AND isset($_GET['action']=='complete') also? He might check only for the 2nd.
Link to comment
Share on other sites

If the code only tests $_GET['action'] == "complete" , if $_GET['action'] is not set, then the test will throw a warning. Warnings do not stop execution, so a lot of people ignore them. Good developers write code that prevents warnings. That is why the code tests isset($_GET['action']) first.

Link to comment
Share on other sites

If the code only tests $_GET['action'] == "complete" , if $_GET['action'] is not set, then the test will throw a warning.
Υes, but I thought...that from the moment we have $_GET['action'] == "complete" we also have(by definition) $_GET['action']This is something I do not understand.
Link to comment
Share on other sites

If it is set to complete then yeah, it's obviously going to be set and isset is going to return true. If it's not set at all then trying to check for the value is going to be an error. So the correct way is to check if it is set first, and then check the value. If it is not set then checking if is set will return false and so it will never check the value which would otherwise cause an error if it's not set at all. It's not about writing code for the situation where everything is fine, it's about writing code that is not going to throw an error in any situation.

Link to comment
Share on other sites

The point they're trying to make is, what if $_GET['action'] did not exist in the array, and all you did was just check for its value?It's called coding defensively.

Link to comment
Share on other sites

I know that get is a superglobal array and that is seen anywhere in the script. I know also(and tell me if I am wrong) that it is related with links...I mean you a pass a value to GET with a link. Recently though, I came across with a scenario where the input of a user in an input box was grabbed by a GET['action'] variable. Here is the HTML-it is a tutorial on AJAX:
<form name="autofillform" action="autocomplete.php">			<table border="1" cellpadding="5">				<thead>					<tr>											<th><strong>Composer Name:</strong></th>						<th><input type="text"  size="40" id="complete-field"  onkeyup="doCompletion();"></th>					</tr>				</thead>				<tbody>				  				</tbody>			</table>		</form>

IN general do user inputs in an input box(search...as in this example here) get "grabbed" from GET['action']?

With GETYou need;To specify the method.and the action.The form will then (Upon submission) go to the urlIn your case;autocomplete.php?complete-field={INPUT HERE}then you can use getfor;$_GET['complete-field']and that will give you the variable defined in the URL by the form.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...