SlideShare une entreprise Scribd logo
1  sur  4
Télécharger pour lire hors ligne
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.

Contenu connexe

Tendances (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
 

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum ComputingGDSC PJATK
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8DianaGray10
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?SANGHEE SHIN
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding TeamAdam Moalla
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfAnna Loughnan Colquhoun
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxMatsuo Lab
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsSeth Reyes
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 

Dernier (20)

Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8UiPath Studio Web workshop series - Day 8
UiPath Studio Web workshop series - Day 8
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team9 Steps For Building Winning Founding Team
9 Steps For Building Winning Founding Team
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdf
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Introduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptxIntroduction to Matsuo Laboratory (ENG).pptx
Introduction to Matsuo Laboratory (ENG).pptx
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Computer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and HazardsComputer 10: Lesson 10 - Online Crimes and Hazards
Computer 10: Lesson 10 - Online Crimes and Hazards
 
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 

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.