How to check availability of an ID in same page? - php

To be specific, I want to make code like Yahoo's Registration (Yahoo ID - Check Availability) checking the availability. The problem here is I need to use PHP to connect, get, and search/verify if the entered value in the textbox is unique.
I tried passing a JavaScript value to a PHP variable, but based on my research, doing this isn't possible, so I need another possible solution to get the value of the textbox and set it to a PHP variable.
Thank you in advance.

The only way to get data from JavaScript to PHP (in a typical environment) is to deliver it over HTTP. Doing this without leaving the page is known as Ajax, and usually done using XMLHttpRequest.
There are no shortage of tutorials describing how to do this, nor any shortage of libraries that will help (plus, of course, all the large libraries such as YUI and jQuery).

You can use Ajax for manipulating this situation.. Have a look at following link.
http://www.digimantra.com/technology/javascript/check-username-availability-validation-ajaxphp/

You need to use AJAX for that.
AJAX is used to send and receive data from the server. So, on the server side, you can create a URL that checks whether a username (can be passed using get/post) is available or not & accordingly send a response. At the client side, depending on the response you receive from the server, you can alert the user.

Related

php ajax multiple requests

I been learning php and ajax from w3schools but I have come across a simply question for which I cant find an answer to.
To request something from a php file I use a xmlhttpRequest object and specify the url (of that php file). Does this mean one php file for one request only? Let's say on a webpage there are a user log-in box and a comment box, I would need two php files to take the requests? I always thought the server side will have one main file that handle all the requests, each request from client will have a ID to specify what the request is and send back the necessary data to client. So what is the right?
I read a lot of material online, but everything is just basic example with just one request and one response.
You can use the same file for multiple requests. You can supply parameters along with the AJAX request, either by including them in the URL after ? (they'll be available in $_GET and $_REQUEST) or by using the POST method and sending them as form data (they'll be available in $_POST and $_REQUEST). You can use the Javascript FormData API to encode this properly; see the documentation here. Using the jQuery library can simplify all of this.
One of the parameters can then be a command or operation code, and the script can take different actions based on this.

Using javascript to get server information

I'm trying to rewrite a snipped of PHP into JavaScript, ideally passing as few as possible variables between the two. Are there any super-global variables in JavaScript that give similar information to the PHP $_SERVER array?
In JavaScript, location seems to give me a bit of useful information but what about something like the PHP equivalent of $_SERVER['HTTP_REFERER']? I also have jQuery as a resource.
window.location can be read or set and comes with properties such as .pathname, .search, .protocol, et cetera -- each of which will provide that particular aspect of the URL as a read-only value.
document.referrer will provide the referring URL as a string. If you want the domain/path/query/et cetera to be separated from one another, you will need to do that yourself, or use a library which will provide it.
document.cookie will provide you a semicolon-delimited list of user/server-set cookies.
Again, turning that into an array or an object is on your own shoulders.
Your cookie string also has no access to expiration-times, nor applicable paths the particular cookie is set for -- security.
For most of the rest of the data, you're going to have to talk to the server -- the browser likes to keep client-side script in the dark about things (like the user's IP, or session-variables, or anything else which can be turned into a security-risk).
This will get you the referrer -
document.referrer
But I think you are looking at this in the wrong way - JavaScript and jQuery are run client side - if you want server variables accessible - you'll have to pass them to the script - possible by means of an AJAX call. You say that you have jQuery available - so you can use the .ajax() function to retrieve all the server data you'll need.

Way to send POST data to a PHP file from another PHP file?

Alright, so I've looked at a ton of questions, but I only found 1 that resembled what I am trying to do. Here is the link to it: Passing POST data from one web page to another with PHP
I want to pass data from one PHP file(we'll call it editData.php) to another PHP file(we'll call it submitData.php). Neither file has any HTML elements (pure PHP code I mean). The first file(editData.php) receives $_POST data, edits it, and needs to send it to the second file. The second file(submitData.php) needs to be able to read in the data using $_POST. No sessions nor cookies can be used I'm afraid.
In the linked question above, the answer accepted was to create hidden fields inside a form and POST the data from there. This worked for the OP because he had user interaction on his "editData.php", so when the user wanted to go to "submitData.php", he would POST the data then.
I can't use this solution(at least, I don't think I can), because I am accessing (and sending $_POST data to) editData.php from a javascript AJAX call and there will be no user interaction on this page. I need the modified data to be POSTed by code, or some other way that does the transfer 'automatically'(or 'behinid-the-scenes' or whatever you want to call it). submitData.php will be called right after editData.php.
I don't know if I can rewrite submitData.php to accept GET data, so count that out as well (it's a matter of being able to access the file). I really don't want to echo stuff back to my original JavaScript function(and then AJAX again). I am encrypting info in editData.php, and (while it sounds silly to say it) I don't want to make it easy for someone to develop a cipher for my encryption. Returning values after being encrypted(viewable with Inspect Element) would make it too easy to decipher if you ask me.
I feel like this issue could come up a lot, so I'd expect that there is something obvious I'm missing. If so, please tell me.
tl;dr? How can I send data to a PHP file via the POST method while only using code in another PHP file?
Well you might consider just streamlining your approach and including the submitData logic at the end of the editData file. But assuming that this is not possible for some reason (files live on different systems, or whatver), your best bet might be to use cURL functionality to post the data to the second script.
If the files are on the same server though I would highly recommend not posting the data to the second script as this will basically just double the amount of requests your web server needs to handle related to this script.

PHP and AJAX Web Content

I am thinking of developing a website in PHP and I was thinking of using AJAX in order to send data to, and retrieve data from a server asynchronously (in the background) without interfering with the display and behavior of the existing page.
So my question: would it be better to use AJAX to GET or POST to php pages or any other options are possible?
There are not that many options when it comes to submiting data, either you use AJAX or use the normal http message like GET or POST. If you would like to determin witch is better i would say: it depends.
Ajax seems to me like a great way of making more dynamic a part of your site, but i dont think its always practical to use in all your site. Ajax is generally used when you need to show some context change on one page, like posting a commner, faving a question, or things like that. Another great thing about it is not to bound user to a form (you can save info with a link like when you vote a question here)
Using GET or POST its usefull to let the user now here's moving along a process or when things that happen after or before are different, or context change a lot when submitting.
Rule of thumb (regardless of AJAX).
If you're going to send large amounts of data, or sensitive data... POST.
Otherwise, GET works just as fine.
I recommend use JQuery.
with $.ajax of Jquery, you can use it with multiple options depending that you need.
So you can work with POST, GET, receive data like text, json...
Here you can get more info:
http://api.jquery.com/jQuery.ajax/
I always use $_POST. I wish I could give a reason why I think it's better, but I can't. I guess it's because I've always preferred sending data via POST rather than GET so the user doesn't see it, and it just carried over to my ajax.
I would use $_POST just for the fact that it can store more data.

jQuery posting variables within the same file

I have some variables set in Javascript. Further down the script I want to use these values in PHP. I realise I need to POST/GET them with jQuery, but I don't understand either function fully, even after looking at the manuals.
Could somebody break it down and explain the parameters?
Would I be better off using GET or POST in the instance?
Can the URL specified be the same as the current page e.g. index.php?
Thanks very much for your help.
You can not do this unless PHP is writing the javascript. PHP is on the server side and will be parsed before Javascript is ever seen by the client. Any variables set by JS will NOT be seen by PHP on the same request.
It's really just a question of style, really.
GET places all key/value-pairs in the URL field, whereas POST puts it in the HTTP body. Since URLs are limited in length, POST is preferred for longer, larger sets of data or data needing to benefit from TLS/SSL encryption.
So let's say we have a key: articleID. You want to pass 1 to articleID, so that the backend can contact the database and retrieve the article in question.
If you make a GET request, you'd invoke the following URL:
index.php?articleID=1
If you use POST, you'll put the data in the request body itself, so you wouldn't be able to tell what value you sent to the server without opening the packet in question and examining the request.
You'll find more information on how to perform these requests back at jQuery's reference site. More information about GET and POST.
You are the architect of the application, so you would know best what method to use. As for contacting the view itself, it's certainly possible albeit questionable from an architectural point of view.

Categories