Use jquery variable in php

Problem in passing Jquery variable to php .

When i pass variable id to B.php i am getting this error

"Notice: Undefined index: id1 in C:\xampp  \htdocs\PhpProject1\OtherUsableItems\B.php on line 2 

How to solve this problem ???? This A.php





 
 
 

This B.php

  

I want id variable to pass on to B.php

Use jquery variable in php

Mr. Alien

149k33 gold badges288 silver badges272 bronze badges

asked Apr 5, 2013 at 11:39

3

Try this one:





 
 
 

Then B.php


answered Apr 5, 2013 at 11:48

Try changing the input type to button instead of submit

answered Apr 5, 2013 at 11:44

The problem is that your form is submited ,$.post is for ajax sumit. For that you need to prevent form submit.


Add event.preventDefault(); in your button click function

Use jquery variable in php

Lodder

19.7k10 gold badges58 silver badges96 bronze badges

answered Apr 5, 2013 at 11:48

Shijin TRShijin TR

7,26010 gold badges47 silver badges115 bronze badges

2

Use a hidden field to set the value you want to send across. Since you are using submit, your entire form is submitted. The hidden field is also sent and this can be accessed directly using the name of the hidden field.

answered Apr 5, 2013 at 11:49

AashrayAashray

2,71515 silver badges22 bronze badges

2

$(document).ready(function(){
  var id = 23; 
  $("#submit").click(function(e){
     $.post("B.php", {id1: id} );     
     e.preventDefault();
   });
 });

Try code above. First: JS does not allow to do something like this: {id1: id,} (see comma at the end). That is a feature of PHP. Actually, your JS code should not be executed at all because that is a syntax error (suppose you will find it in console of your browser)

Another problem: you must prevent default submit of a form. e.preventDefault() is needed for that. Without it, ajax post will be started and than usual form submit will happen. And your form has no element with name id1, suppose that second request produce that error message.

EDIT: This is to post a value with regular submit, so browser is redirected to B.php

$(document).ready(function(){
      var id = 23; 
      $("#submit").click(function(){
              $(this).closest("form")
                     .append($("", {"type":"hidden", "name":"id1", "value":id}));
       });
     });

answered Apr 5, 2013 at 11:44

Viktor S.Viktor S.

12.6k1 gold badge26 silver badges51 bronze badges

2

Try this code,

 
 
 
 
 
 
 

Here you can pass value of 'id' to B.php,And will be access as $_POST['id1']

answered Apr 5, 2013 at 12:08

Shijin TRShijin TR

7,26010 gold badges47 silver badges115 bronze badges

Try this:


Since you are using ajax feature so you have to stop the form submit feature. You have a trailing comma here {id1 : id,} this is an issue in IE browsers but latest other browsers will just work fine.

Note:

You posted this error:

"Notice: Undefined index: id1 in C:\xampp \htdocs......

Don't run the page in filesystem $.post() won't work this way.

Instead try using http://localhost/yourapplication/page.php

answered Apr 5, 2013 at 11:44

Use jquery variable in php

JaiJai

73.4k12 gold badges73 silver badges99 bronze badges

4

Or you could use submit() this way

$(function(){
    var id = 23;
    $("#myform").submit(function() {
        $.post("B.php", { id1: id }).done(function(data) {
            alert( data );
        });
        return false; //true(post form data)
    });
});

And form

EDIT: Oh and yeah for B.php isset check or error_reporting(0);

if(isset($_POST['id1'])){
    print_r( $_POST );
}

answered Apr 5, 2013 at 12:28

ZhamaZhama

12 bronze badges

Not the answer you're looking for? Browse other questions tagged php javascript jquery or ask your own question.

Can we use jQuery variable in PHP?

You cannot use javascript variable in php. However you can use php variable within javascript. Javascript is client side and php is server side, if you want to use then send with ajax because its like sending from client to server.

How do you pass a jQuery variable to PHP and then display it?

By adding e.preventDefault(); Error has been resolved but after submitting it should go to B.php and echo value of variable a. – Abhishek. Apr 5, 2013 at 11:58..
Then,You need to remove ajax,$.post is used for ajax submit.that will not redirect to B.php.That create xmlhttp request. – Shijin TR. Apr 5, 2013 at 12:11..

Can I use Javascript variable in PHP?

The way to pass a JavaScript variable to PHP is through a request. This type of URL is only visible if we use the GET action, the POST action hides the information in the URL. Server Side(PHP): On the server side PHP page, we request for the data submitted by the form and display the result. $result = $_GET [ 'data' ];

How can get jQuery variable value in PHP without ajax?

Create an hidden input tag in HTML , then set it's value as a JSON string with jquery. Next whenever you need that data , simply call the PHP function which returns value of input tag ..
var jsdata = {.
data: "Test".
var jsondata = $. toJson(jsdata);.
$("#transferInput"). val(jsondata);.