1

here is the code.......here i am passing a javascript array to php... something is going wrong........

html:

    <html>
    <body onload="aa()" >
<script language="javascript">
function aa()
{
var a, s;
a = [ 'one', 'two', 'three'];
s = JSON.stringify( a );
document.getElementById("fb_user").innerHTML= s;
}

    </script>
<form action="check1.php" method="post">
    <label id="fb_user"> </label>
<input type="submit" value="submit"/>
</form>
</body>
</html>

php code:

<?php

$fb_usr_id =json_decode(  $_POST['fb_user'], true );

    echo $fb_usr_id;
    ?>

when $fb_usr_id is echoed nothing is printed??

6
  • You are adding the JSON string to the label, instead of the input element. Commented Jun 13, 2011 at 16:13
  • @Pekka what u say pekka. cannot understand.....? Commented Jun 13, 2011 at 16:32
  • The code you have provided just creates a form it does not SUBMIT that form. Are you SUBMITting it manually? Commented Jun 13, 2011 at 16:37
  • @James it submits the form.. why u say it is not submitting the form Commented Jun 13, 2011 at 16:39
  • Your code does this- create a form, populate the form with some stuff. It is up to the user to click the submit button. Is that correct? Commented Jun 13, 2011 at 16:42

4 Answers 4

2

Contents of label tags are not submitted.

Try changing
<label id="fb_user"> </label> to
<input type="hidden" name="fb_user" id="fb_user">

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

3 Comments

also change document.getElementById("fb_user").innerHTML= s; with document.getElementById("fb_user").value= s;
@venimus Absolutely not, the JavaScript function is called by onload.
Guess I should have mentioned the JS change. Anyway, use Firebug or equivalent tool to ensure the contents of the hidden field are being updated correctly before form submission.
1
    <html>
    <body onload="aa()" >
<script language="javascript">
function aa()
{
var a, s;
a = [ 'one', 'two', 'three'];
s = JSON.stringify( a );
document.getElementById("fb_user").value = s;
}

    </script>
<form action="check1.php" method="post">
<input type="hidden" name="fb_user" id = "fb_user" value="">
<input type="submit" value="submit"/>
</form>
</body>
</html>

On the backend do this first to check the validity of the posted variable as being json

var_dump( $_POST );

2 Comments

when i put var_dump( $_POST ); in php file. i get : array(0) { }
make sure that the var_dump() I mentioned is on the page check1.php. As as sanity check, put the word "test" as the value in the hidden field temporarily then you will have identified if it is a PHP or JS problem... and as others have pointed out you need to press the submit button manually before this will work.
0

The contents of labels are not posted to the server. Instead, use <input type="hidden"> for inputs not intended to be editable by users:

<input name="fb_user" id="fb_user" type="hidden" />

Also, you should not change the innerHTML if you just want set the value of an input element. Instead, assign the aptly named value property.

5 Comments

@ask123 Can you post a complete example code or a URL to the running instance?
@ask123 Did you maybe forget to set the name attribute of the input element?
@ask123 You should remove the closing </label>. With that exact code , I get the output Array (php's string representation of an array).
@ask123 You may want to include var_dump($_POST); in your php code to catch all output.
@phihag i have remove </label> but nothing is printed??
0

Here is how you should do it:

<html>
    <body>
    <form action="check1.php" method="post">
        <label id="fb_users_list"></label>
        <input type="hidden" name="fb_user" id="fb_user" value="" />
        <input type="submit" value="submit"/>
    </form>
    <script type="text/javascript">
        (function(){
            var a, s;
            a = [ 'one', 'two', 'three'];
            s = JSON.stringify( a );
            document.getElementById("fb_users_list").innerHTML = s;
            document.getElementById("fb_user").value = s;
        })();
    </script>
    </body>
</html>

Please do not use body onload="", because you open up holes to XSS attack. Just put your script below your form, or better of at the end of the body, and you won't need an onload event.

4 Comments

ya shef i had removed onload. but nothing display. any suggestion??
@ask123 did you try my code above? Is it displaying anything at all before you submit, or not?
when i click submit button .the php check1.php executes and displays : Array . what does this mean
@ask123: Do you need to have the fb user ids in an array, or you can have them in a list as well? I mean can you change this a = [ 'one', 'two', 'three']; to this a = 'one, two, three';?

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.