0

I want to assign JQuery variable to the PHP variable. Any idea kindly

<?php    
  $phpvariable = jqueryvariable;    
?>
3
  • 6
    can't be done. because one runs at server and other is on browser. what you can do is either post a form or use ajax. Commented Nov 2, 2015 at 9:45
  • @jai any other idea i get jquery variable on click function Commented Nov 2, 2015 at 9:46
  • You should explain your problem, and why do you want to do this. Commented Nov 2, 2015 at 9:56

2 Answers 2

2

The restriction is that you are assigning a value from frontend to backend and this can't be done because php runs at server side while js/jquery runs in the browser.

If you are using ajax then you can use the posted value as:

<?php

$phpvariable = $_REQUEST['jqueryvariable'];

?>

Assume this is the posted data in the ajax:

data:{ jqueryvariable:"theDummyVar" }  

or without ajax:

use a <input type="hidden" name="jqueryvariable" value="theDummyVar" /> and post it to the php. This needs to be in the form element.

Sign up to request clarification or add additional context in comments.

2 Comments

then you can use form to submit the values. @WaqasKhan
and you can use [type="hidden"] if you are having some value which you don't want to show your user, otherwise you can use your normal input textfields.
0

Why do you need to refer a jquery variable into phpvar? You can just pass the jquery variable via ajax post or you can do it this way.

<?php
$phpvariable = $_POST['textfieldId'];
?>

3 Comments

i think it should be textfieldname not ID
If you are not using Ajax the code above should work. As per Jai, you cannot directly relate the variables of front-end to backend.
@n00bster perhaps you want to change this () to [].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.