0

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);
10
  • </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); Commented Jul 10, 2013 at 8:52
  • what is the result of echo $_POST['hiddenF'];? Commented Jul 10, 2013 at 8:55
  • </form> maybe in the future!!, i have also tried it but got no result with $arr=json_decode($_POST['hiddenF'],true); !! Commented Jul 10, 2013 at 8:56
  • doesn't onSubmit need to return "true" ? Commented Jul 10, 2013 at 8:58
  • 2
    Are you sure the function fun() is being executed? Commented Jul 10, 2013 at 8:59

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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);

Comments

0

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);
?>

Comments

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.