SlideShare a Scribd company logo
1 of 4
Download to read offline
JQuery POST method in PHP
Many of you already know about JQuery and its utilities. For the ones who are still unaware of
the JQuery library and its usage, you can go through this article to get a brief introduction on the
matter. Of course there is always the official website for you to refer to.
The POST method in JQuery handles AJAX calls to transfer data from one file to another. This
function is similar to a POST method of that of a simple HTML form. The data passes according
to the field and variable names and is collected in a PHP file at the server end. You might declare
the POST method from a PHP file or an HTML file.
Also read my article on AJAX database update with JQuery POST
The difference lies in the fact that – for form POST methods in plain HTML, the variables are
passed with page navigation. The server side file is loaded and then the data is collected and
worked upon. However, the JQuery POST method uses an asynchronous method of sending the
data. So, there is no question of page load or navigation. The control stays on the same page.
Thus the process is faster and does not trouble the speed bandwidth.

JQuery POST method basics
The JQuery POST method is simple to call and use. There are two methods of calling the
method. Here are the two methods mentioned –
$.ajax(
type: „POST‟,
url: „save.php‟,
data: {name:name,password:password},
dataType: 'HTML',
success: function(value){
// Do something here
}
);

And the other method is –
$.post(„save.php‟,{name:name,password:password},function(value){
// Do something here.
});

As you can see, the calling of the JQuery POST method is very simple. For your understanding,
let me explain each of the section clearly.
For the first method of calling, the AJAX method is called. The method type is set to be POST.
You can set it as GET as well if you want a GET method type. Here GET or POST does not
matter as much because here the data is passed to the server in an asynchronous manner. So,
there is no chance of the data getting showed up in the URL. Next we have ‘url’. This URL is the
path to the PHP file that receives the data from here to manipulate or work upon.
The ‘data’ is the values that we want to send. Always remember, this data gets transported as
JSON and the variable name that you set here must match the name of the variable in the PHP
file that receives the respective value.
The ‘dataType’ parameter indicates the type of data that is returned back to this current file from
the server end upon successful transmission of the data to the server file. By default, this is set to
HTML. You can set it as JSON or XML. The return value will automatically change.
Suppose you have a scenario where you send a data to the server PHP file and query something
from the database. After this, instead of appending a static content to your current HTML page
you want to append a dynamic value from the server end. This is when you require JSON. I will
elaborate on this a bit later.
Finally, the ‘success’ parameter defines the function that is called when a successful sending is
done. Normally the value that comes back in the ‘dataType’ format is worked upon in this
callback success function.
Now, you can easily make out that in the second form of declaration the format is a bit different
but the parameters are same. The ‘url’ parameter comes first, then the ‘data’ and then the
callback ‘success’ function. This is actually shorthand syntax. The ‘dataType’ parameter is not
explicitly mentioned because normally developers make use of the HTML return value. If you
need JSON value, just do the following –
$.post(„save.php‟,{name:name,password:password},function(value){
// Do something here
},‟JSON‟);

Common examples of JQuery POST method
The POST method comes very handy when you require server side interference. Otherwise, for
any static data you would never require this method. One of the most used techniques is
validating new usernames or emails with the existing user base. Several websites implement this.
The idea is as soon as the user starts typing in the username of their choice, the server is passed
the current value and a message is appended instantly to notify whether this username is
available or not. I have already discussed and given an example of this working in this article.
Check it out.
Another good example of JQuery POST method usage would be using a JSON return type. This
is when you want the user to see a dynamic value (a value from the database according to the
current user entry) when a certain event is performed.
For example, you enter the name of a student and you get to see which subjects he or she has
opted for – instantly! Yes that is only possible with a JQuery POST method with JSON return
type. The value passed back will be in the form of a JSON array, you need to access the values
and get them appended to the HTML. This is not possible for you to achieve in a simple .html
file unless you add a JQuery POST method. Here’s a demo code // The JQuery code in the HTML file
$(„#textbox‟).keydown(function(){
$.post(„subjects.php‟, {name:name}, function(value){
for(var i=0;i<value.length;i++){
$(„#subjects ul‟).append(„<ul style="text-align: justify;">
<li>‟+value[i]+‟</li></ul>‟); } }, „json‟);
});
// The PHP code (assuming that DB is connected)
$name = $_POST[„name‟];
$query = “SELECT subjects FROM students WHERE name=‟”.$name.”‟”;
$result = mysql_query($query)
or die();
$sub = mysql_fetch_assoc($result);
echo json_encode($sub);

Common errors in JQuery POST method
When you try using this method always keep in mind the following points. These are some of the
most commonly arising errors that I have faced. So, I thought of letting you guys know so that
you do not face same trouble.
- Make sure that while sending the ‘data’ in the JQuery POST method, the variable name that
comes on the left of each JSON array element composition is the name that the program expects
to be as the PHP file receiving variable name. Let’s make it simpler. Check the following code –
// if this your POST method „data‟
{a:name,password:b}

Then, the PHP file expects that when you receive the values as $_POST[], the variable that
accepts the value will have the same name as the left component. The right component is the
JavaScript variable. In this case, ‘name’ and ‘b’ are the JavaScript variables. So in the PHP file
we write –
$a = $_POST[„a‟]; $password = $_POST[„password‟];

- Always make sure the URL to the PHP file is correct.
- If in case you have a success function and it’s not working; try removing the statements inside
the function and make an alert. If the alert happens then there is something wrong in the
statements inside your callback function.
If the alert does not happen, then the program control is getting stuck in the PHP file. Check that
file. If it’s an issue with the PHP file, check the queries and the return values. They often create a
tantrum.
That’s all about this method. Try and let me know what you did.

More Related Content

What's hot (20)

JSON
JSONJSON
JSON
 
JSON
JSONJSON
JSON
 
Json
JsonJson
Json
 
Json Tutorial
Json TutorialJson Tutorial
Json Tutorial
 
JSON and XML
JSON and XMLJSON and XML
JSON and XML
 
Json
JsonJson
Json
 
JSON and REST
JSON and RESTJSON and REST
JSON and REST
 
Json
JsonJson
Json
 
Json
JsonJson
Json
 
Php forms
Php formsPhp forms
Php forms
 
java script json
java script jsonjava script json
java script json
 
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
What is JSON? Why use JSON? JSON Types? JSON Helpful Tools?
 
Xml
XmlXml
Xml
 
JSON - Quick Overview
JSON - Quick OverviewJSON - Quick Overview
JSON - Quick Overview
 
Pollock
PollockPollock
Pollock
 
MuleSoft DataWeave data transformation language
MuleSoft DataWeave data transformation languageMuleSoft DataWeave data transformation language
MuleSoft DataWeave data transformation language
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)Php, mysq lpart4(processing html form)
Php, mysq lpart4(processing html form)
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 5
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 

Viewers also liked

Seo presentation dm10
Seo presentation   dm10Seo presentation   dm10
Seo presentation dm10Rune Risom
 
Kom godt i gang med: At få en god plads på Google
Kom godt i gang med: At få en god plads på GoogleKom godt i gang med: At få en god plads på Google
Kom godt i gang med: At få en god plads på GoogleRune Risom
 
Spring-Summer 2011 (part 1)
Spring-Summer 2011 (part 1)Spring-Summer 2011 (part 1)
Spring-Summer 2011 (part 1)Babyholiday Btq
 
Node.js: perche' tutto questo hype?
Node.js: perche' tutto questo hype?Node.js: perche' tutto questo hype?
Node.js: perche' tutto questo hype?Giancarlo Valente
 
What's so special about node.js
What's so special about node.jsWhat's so special about node.js
What's so special about node.jsGiancarlo Valente
 

Viewers also liked (8)

Seo presentation dm10
Seo presentation   dm10Seo presentation   dm10
Seo presentation dm10
 
Kom godt i gang med: At få en god plads på Google
Kom godt i gang med: At få en god plads på GoogleKom godt i gang med: At få en god plads på Google
Kom godt i gang med: At få en god plads på Google
 
Badly Drawn Boy - Samsonic
Badly Drawn Boy - SamsonicBadly Drawn Boy - Samsonic
Badly Drawn Boy - Samsonic
 
Spring-Summer 2011 (part 1)
Spring-Summer 2011 (part 1)Spring-Summer 2011 (part 1)
Spring-Summer 2011 (part 1)
 
Node.js: perche' tutto questo hype?
Node.js: perche' tutto questo hype?Node.js: perche' tutto questo hype?
Node.js: perche' tutto questo hype?
 
What's so special about node.js
What's so special about node.jsWhat's so special about node.js
What's so special about node.js
 
Iffis14 datavisualisering
Iffis14 datavisualiseringIffis14 datavisualisering
Iffis14 datavisualisering
 
Mobil app
Mobil appMobil app
Mobil app
 

Similar to J query post method in php

Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptxSherinRappai
 
How to insert json data into my sql using php
How to insert json data into my sql using phpHow to insert json data into my sql using php
How to insert json data into my sql using phpTrà Minh
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiNaveen Kumar Veligeti
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptxitzkuu01
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Pranav Prakash
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/tutorialsruby
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erickokelloerick
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07Hassen Poreya
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Adnan Sohail
 
Programming with php
Programming with phpProgramming with php
Programming with phpsalissal
 

Similar to J query post method in php (20)

forms.pptx
forms.pptxforms.pptx
forms.pptx
 
Working with data.pptx
Working with data.pptxWorking with data.pptx
Working with data.pptx
 
How to insert json data into my sql using php
How to insert json data into my sql using phpHow to insert json data into my sql using php
How to insert json data into my sql using php
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Ajax
AjaxAjax
Ajax
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
Unit-5.pptx
Unit-5.pptxUnit-5.pptx
Unit-5.pptx
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7Implementing Ajax In ColdFusion 7
Implementing Ajax In ColdFusion 7
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
&lt;b>PHP&lt;/b>/MySQL &lt;b>Tutorial&lt;/b> webmonkey/programming/
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07
 
Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)Asynchronous JavaScript & XML (AJAX)
Asynchronous JavaScript & XML (AJAX)
 
Core Java tutorial at Unit Nexus
Core Java tutorial at Unit NexusCore Java tutorial at Unit Nexus
Core Java tutorial at Unit Nexus
 
Programming with php
Programming with phpProgramming with php
Programming with php
 
AJAX.pptx
AJAX.pptxAJAX.pptx
AJAX.pptx
 

Recently uploaded

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 

Recently uploaded (20)

Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 

J query post method in php

  • 1. JQuery POST method in PHP Many of you already know about JQuery and its utilities. For the ones who are still unaware of the JQuery library and its usage, you can go through this article to get a brief introduction on the matter. Of course there is always the official website for you to refer to. The POST method in JQuery handles AJAX calls to transfer data from one file to another. This function is similar to a POST method of that of a simple HTML form. The data passes according to the field and variable names and is collected in a PHP file at the server end. You might declare the POST method from a PHP file or an HTML file. Also read my article on AJAX database update with JQuery POST The difference lies in the fact that – for form POST methods in plain HTML, the variables are passed with page navigation. The server side file is loaded and then the data is collected and worked upon. However, the JQuery POST method uses an asynchronous method of sending the data. So, there is no question of page load or navigation. The control stays on the same page. Thus the process is faster and does not trouble the speed bandwidth. JQuery POST method basics The JQuery POST method is simple to call and use. There are two methods of calling the method. Here are the two methods mentioned – $.ajax( type: „POST‟, url: „save.php‟, data: {name:name,password:password}, dataType: 'HTML', success: function(value){ // Do something here } ); And the other method is – $.post(„save.php‟,{name:name,password:password},function(value){ // Do something here. }); As you can see, the calling of the JQuery POST method is very simple. For your understanding, let me explain each of the section clearly. For the first method of calling, the AJAX method is called. The method type is set to be POST. You can set it as GET as well if you want a GET method type. Here GET or POST does not matter as much because here the data is passed to the server in an asynchronous manner. So,
  • 2. there is no chance of the data getting showed up in the URL. Next we have ‘url’. This URL is the path to the PHP file that receives the data from here to manipulate or work upon. The ‘data’ is the values that we want to send. Always remember, this data gets transported as JSON and the variable name that you set here must match the name of the variable in the PHP file that receives the respective value. The ‘dataType’ parameter indicates the type of data that is returned back to this current file from the server end upon successful transmission of the data to the server file. By default, this is set to HTML. You can set it as JSON or XML. The return value will automatically change. Suppose you have a scenario where you send a data to the server PHP file and query something from the database. After this, instead of appending a static content to your current HTML page you want to append a dynamic value from the server end. This is when you require JSON. I will elaborate on this a bit later. Finally, the ‘success’ parameter defines the function that is called when a successful sending is done. Normally the value that comes back in the ‘dataType’ format is worked upon in this callback success function. Now, you can easily make out that in the second form of declaration the format is a bit different but the parameters are same. The ‘url’ parameter comes first, then the ‘data’ and then the callback ‘success’ function. This is actually shorthand syntax. The ‘dataType’ parameter is not explicitly mentioned because normally developers make use of the HTML return value. If you need JSON value, just do the following – $.post(„save.php‟,{name:name,password:password},function(value){ // Do something here },‟JSON‟); Common examples of JQuery POST method The POST method comes very handy when you require server side interference. Otherwise, for any static data you would never require this method. One of the most used techniques is validating new usernames or emails with the existing user base. Several websites implement this. The idea is as soon as the user starts typing in the username of their choice, the server is passed the current value and a message is appended instantly to notify whether this username is available or not. I have already discussed and given an example of this working in this article. Check it out. Another good example of JQuery POST method usage would be using a JSON return type. This is when you want the user to see a dynamic value (a value from the database according to the current user entry) when a certain event is performed. For example, you enter the name of a student and you get to see which subjects he or she has opted for – instantly! Yes that is only possible with a JQuery POST method with JSON return
  • 3. type. The value passed back will be in the form of a JSON array, you need to access the values and get them appended to the HTML. This is not possible for you to achieve in a simple .html file unless you add a JQuery POST method. Here’s a demo code // The JQuery code in the HTML file $(„#textbox‟).keydown(function(){ $.post(„subjects.php‟, {name:name}, function(value){ for(var i=0;i&lt;value.length;i++){ $(„#subjects ul‟).append(„<ul style="text-align: justify;"> <li>‟+value[i]+‟</li></ul>‟); } }, „json‟); }); // The PHP code (assuming that DB is connected) $name = $_POST[„name‟]; $query = “SELECT subjects FROM students WHERE name=‟”.$name.”‟”; $result = mysql_query($query) or die(); $sub = mysql_fetch_assoc($result); echo json_encode($sub); Common errors in JQuery POST method When you try using this method always keep in mind the following points. These are some of the most commonly arising errors that I have faced. So, I thought of letting you guys know so that you do not face same trouble. - Make sure that while sending the ‘data’ in the JQuery POST method, the variable name that comes on the left of each JSON array element composition is the name that the program expects to be as the PHP file receiving variable name. Let’s make it simpler. Check the following code – // if this your POST method „data‟ {a:name,password:b} Then, the PHP file expects that when you receive the values as $_POST[], the variable that accepts the value will have the same name as the left component. The right component is the JavaScript variable. In this case, ‘name’ and ‘b’ are the JavaScript variables. So in the PHP file we write – $a = $_POST[„a‟]; $password = $_POST[„password‟]; - Always make sure the URL to the PHP file is correct. - If in case you have a success function and it’s not working; try removing the statements inside the function and make an alert. If the alert happens then there is something wrong in the statements inside your callback function. If the alert does not happen, then the program control is getting stuck in the PHP file. Check that file. If it’s an issue with the PHP file, check the queries and the return values. They often create a tantrum.
  • 4. That’s all about this method. Try and let me know what you did.