Passing an Array Using an HTML Form Hidden Element

Passing an array using an HTML form hidden element

Use:

$postvalue = array("a", "b", "c");
foreach($postvalue as $value)
{
echo '<input type="hidden" name="result[]" value="'. $value. '">';
}

And you will get $_POST['result'] as an array.

print_r($_POST['result']);

How to pass php array in input type hidden in html form

If I'm understanding you correctly, you can try this:

<form method="post" action="next.php">
<input type="hidden" name="my_form_data" value="<?php echo htmlspecialchars(serialize($my_arr)) ?>">
<button name="submit_btn">Submit</button>
</form>

Then in next.php you unserialize to get the PHP data structure back:

<?php
$my_arr = unserialize($_POST["my_form_data"]);

Is it possible to pass an array in a form as a hidden field?

You can pass the id of the parent job in the hidden field, on form submit action you can get the parent id and retrieve the information of that job.

<input type="hidden" name="parent_job_id" value="<?php echo $jobId; ?>">

Make sure which index holding the id of the parent job.

It seems $job is an array, you can not echo it, you can echo its index.

pass php multidimensional array from html hidden field to php

finally after trying lot of method this one worked for me with using serialize and unseiralize.

HTML:

<input type="hidden" name="data" value="<?php echo htmlspecialchars(serialize($data)) ?>">

PHP:

$excel = unserialize($_POST["data"]);

Passing a multidimensional array through hidden input via POST

You just need to encode array to json and decode it on action page

echo '<input type="hidden" name="itemsArr" value="'.json_encode($itemsArr). '">';

And at your action Page just decode it

$itemsArr = json_decode($_POST['itemsArr'],true)

How can I access an array declared in an HTML hidden input from a Javascript function?

Naming a hidden input sq[] doesn't change it into an array. It's still just a text field essentially. You will need to access the field and parse the value as a comma-separated list (or JSON or whatever format you choose).

Assuming you have a form like so:

<form name="form1">
<input type="hidden" name="sq[]" value="a,b,c,d" />
<input type="hidden" name="sqjson[]" value='["e","f","g","h"]' />
</form>

You can access the values using split [MDN]:

var arr = document.form1['sq[]'].value.split(',');
for (var ii = 0; ii < arr.length; ii++) {
console.log(arr[ii]);
}

Or using JSON.parse [MDN], which would make more complex objects easier to store in a hidden field:

var arr = JSON.parse(document.form1['sqjson[]'].value);
for (var ii = 0; ii < arr.length; ii++) {
console.log(arr[ii]);
}

Click here for a working version of this example.

Passing array to hidden input and retrieve array with elements on different indexes

You can do that in PHP with the function explode. What this function does is split a string into an array by delimiter so in your case, add:

$array = explode(',', $arrayThatHasTheString[0]);

Pass jQuery array value on submit as hidden input

I've seen guys like you trying to code my router interface, so I'll help out.

  1. give your form an id cause you'll need it later

    <form action="script.php" method="post" id="the_form">
  2. add the hidden input in the form

    <input type="hidden" name="values" id="values" value="" />
  3. the button in the form matures to a real submit (amazing)

    <input type="submit" ...
  4. your "getSelectedChbox()" function is amazing; don't change anything there, just wanted to give you congratulations for it, it's a great function

  5. now, where it says document.getElementById('btntest').onclick - get rid of all that and add this code instead; this code will do the rest.

    document.getElementById('the_form').onsubmit = function(){
    var selchb = getSelectedChbox(this);
    var values = selchb.join(', ');

    if(!values.length){
    alert('There was an error. You have to select some checkboxes. ');
    return false;
    }

    document.getElementById('values').value = values;
    if(!confirm(" Are you interested in submitting this form now? If not, click accordingly. "))
    return false;
    }

Or simply copy-paste this whole thing in a file called script.php:

<?php echo var_dump(isset($_POST['values']) ? $_POST['values'] : 'Submit first.'); ?>

<form action="script.php" method="post" id="the_form">
<input type="checkbox" name="chb1" value="html" />HTML<br/>
<input type="checkbox" name="chb2" value="css" />CSS<br/>
<input type="checkbox" name="chb3" value="javascript" />JavaScript<br/>
<input type="checkbox" name="chb4" value="php" />php<br/>
<input type="checkbox" name="chb5" value="python" />Python<br/>
<input type="checkbox" name="chb6" value="net" />Net<br/>

<input type="hidden" name="values" id="values" value="" />
<input type="submit" value="Click" id="btntest" />

</form>

<script type="text/javascript"><!--
function getSelectedChbox(frm) {
var selchbox = [];
var inpfields = frm.getElementsByTagName('input');
var nr_inpfields = inpfields.length;
for(var i=0; i<nr_inpfields; i++) {
if(inpfields[i].type == 'checkbox' && inpfields[i].checked == true)
selchbox.push(inpfields[i].value);
}
return selchbox;
}

document.getElementById('the_form').onsubmit = function(){
var selchb = getSelectedChbox(this);
var values = selchb.join(', ');

if(!values.length){
alert('There was an error. You have to select some checkboxes. ');
return false;
}

document.getElementById('values').value = values;
if(!confirm(" Are you interested in submitting this form now? If not, click accordingly. "))
return false;
}
//-->

</script>

Have fun.



Related Topics



Leave a reply



Submit