Skip to content
Advertisement

How to get javascript value into php variable without reloading the page using ajax

Here I want to get a value from JavaScript to PHP variable without using any POST or GET method.

Here I’m giving HTML code:

JavaScript

Javascript Code

JavaScript

php code

JavaScript

Variable value not working.

Advertisement

Answer

You can not directly assign javascript variable to PHP variable, that’s why ajax is used.

If you want to perform operations on client side variables to server-side without page refresh and using the same page then you have to write the PHP code on the top of the page before anything start of client-side and the use exit to break after the PHP response is completed. and in jquery ajax forget the URL part as you are using the same page for request and response.

Note: Make sure to include jQuery

Cover all the element in form tag so we can simply send data using serialize method.

index.php

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement