php - Not able to pass value from [object HTMLInputElement] - Stack Overflow

I am passing hidden value, and getting it into javascript function. I want to pass this value to anothe

I am passing hidden value, and getting it into javascript function. I want to pass this value to another page. Before passing when i print it, it gives me [object HTMLInputElement]

$var4 = 1;

echo "<input type='hidden' id='var4' value='$var4'>";

and accessing it in javascript

var y = document.getElementById('var4');
   document.write(y);    //[object HTMLInputElement]

it gives me output as [object HTMLInputElement] instead of 1

Another thing is that I want to pass this value to data.php

".... data.php?q="+y)

is this write, can i pass value like this? Would you suggest me any solution. Thanks in advance.

I am passing hidden value, and getting it into javascript function. I want to pass this value to another page. Before passing when i print it, it gives me [object HTMLInputElement]

$var4 = 1;

echo "<input type='hidden' id='var4' value='$var4'>";

and accessing it in javascript

var y = document.getElementById('var4');
   document.write(y);    //[object HTMLInputElement]

it gives me output as [object HTMLInputElement] instead of 1

Another thing is that I want to pass this value to data.php

".... data.php?q="+y)

is this write, can i pass value like this? Would you suggest me any solution. Thanks in advance.

Share Improve this question asked Jun 4, 2010 at 6:25 Rishi2686Rishi2686 1271 gold badge2 silver badges16 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Well, yes, document.getElementById('var4') gets the HTML element with the id 'var4'. It doesn't give you the value of the HTML element, it gives you the whole element.
y.value would give you the value.

I'm not sure I understand the situation correctly, but if you just want to pass variables from PHP to Javascript, you don't need to create hidden input elements to do so. Rather, use something like this:

<script type="text/javascript" charset="utf-8">
  var myVars = <?php echo json_encode($myVars); ?>;
</script>

This way the variables are directly available as Javascript variables, no getting of elements necessary.

document.getElementById returns the html element with the specified id (and hence the HTMLInputElement); read its value to get the required value.

var y = document.getElementById('var4').value;

"...data.php?q="+y is fine. You can read the passed value in the PHP page using $_GET['y']. Just make sure y is not NaN or undefined

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744351549a4570006.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信