Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a javascript array and want to send it to the php via form input hidden field. What i'm doing is here:

html:

<form method="post" action="a.php" onSubmit="fun();">
    <input type="hidden" name="hiddenF" value="">
</form>

and in javascript:

function fun()
{
    var jArray = [ "One", "Two", "Three"];

    document.getElementsByName("hiddenF").value = JSON.stringify(jArray);
}

and finally in php that i need an array doing like below but i don't get anything on page.

$arr=json_decode($_POST['hiddenF']);
print_r($arr);
share|improve this question
</form? should be </form>. also I think that if you want to get an array in php you need to add $arr=json_decode($_POST['hiddenF'],true); – Andreas Lindgren Jul 10 at 8:52
what is the result of echo $_POST['hiddenF'];? – skafandri Jul 10 at 8:55
</form> maybe in the future!!, i have also tried it but got no result with $arr=json_decode($_POST['hiddenF'],true); !! – Hasteh Elmi Jul 10 at 8:56
doesn't onSubmit need to return "true" ? – Paul Jul 10 at 8:58
2  
Are you sure the function fun() is being executed? – Jesbus Jul 10 at 8:59
show 5 more comments

4 Answers

document.getElementsByName("hiddenF") returns an array. So you need to add [0] to access your hidden input.

Should be like that:

document.getElementsByName("hiddenF")[0].value = JSON.stringify(jArray);
share|improve this answer
thank u dear Raman Chodźka – Hasteh Elmi Jul 10 at 9:08
@HastehElmi you are welcome. – Raman Chodźka Jul 10 at 9:11

You can also do it by using ID attribute..This will work

<?php
if(isset($_POST["submit"])){
    $arr = json_decode($_POST['hiddenF']);
    print_r($arr);
}
?>

<script type="text/javascript">
function fun()
{
    var jArray = [ "One", "Two", "Three"];
    document.getElementById("hiddenF").value = JSON.stringify(jArray);
}

</script>

<form method="post" action="<?=$_SERVER["PHP_SELF"]?>" onSubmit="fun();">
    <input type="hidden" name="hiddenF" value="" id="hiddenF">
    <input type="submit" name="submit" />
</form>

If you want to make it work with name attribute, go with above answers.

share|improve this answer

The function document.getElementsByName return an array, so you can't change the .value of this array.

// ...
document.getElementsByName("hiddenF")[0].value = JSON.stringify(jArray);

But I suggest you to use getElementById instead.

HTML:

<input type="hidden" id="hiddenF" name="hiddenF" value="">

JS:

// ...
document.getElementById("hiddenF").value = JSON.stringify(jArray);
share|improve this answer

Try this:

Java Script:

function fun(){
    var jArray = [ "One", "Two", "Three"];
    document.getElementsByName("hiddenF")[0].value = JSON.stringify(jArray);    
}

Html Form:

<form method="post" action="process.php" onSubmit="return fun();">
    <input type="hidden" name="hiddenF" value="">
    <input type="submit" value="Submit"/>
</form>

Process.php

<?php
$arr=json_decode($_POST['hiddenF']);
print_r($arr);
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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