Alistair Weir
Alistair Weir

Reputation: 1849

Form values not passed by jquery submission

I have a form embedded in table that is created on the initial page load:

echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols'>";
echo $resarr[$k]['typ'] . "</td></form>";

I submit the form when a cell in the table is clicked and want to pass the value of the cell that was clicked.

<script>
$(".hols").click(function() {
    $("#showbooking").submit();
})
</script>

After the reload I am testing with this:

echo "<p> ID of clicked booking was: " . $_POST['clickedcalid']; . "</p>";

but the value is not being passed and I'm not sure why.

Possibly because the form is embedded in a table?

Upvotes: 1

Views: 102

Answers (3)

Naumche Ivanovski
Naumche Ivanovski

Reputation: 110

To get value from $_POST you need to have input tag

echo "<form id='showbooking' action='calendar.php' method='post'>";
echo "<td><input type='text' name='clickedcalid' value='" . $resarr[$k]['calid'];
echo "' title='" . $resarr[$k]['coms'] . "' class='hols' />";
echo $resarr[$k]['typ'] . "</td></form>";

Thanks, Naumche

Upvotes: -1

matewka
matewka

Reputation: 10148

You can't put <form> inside a tr element, wrapping td. <tr> specification says

Permitted content: Zero or more <td> or <th> elements, or a mix of them

Also value attribute is not valid for td element. You need to restructure your markup.

echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">' .
    '<form id="showbooking" action="calendar.php" method="post">' .
        '<input type="hidden" name="clickedcalid" value="' . $resarr[$k]['calid'] . '">' .
        $resarr[$k]['typ'] .
    '</form>' .
'</td>';

Upvotes: 1

Rory McCrossan
Rory McCrossan

Reputation: 337570

Firstly, the form element needs to go inside the td. Secondly, only the values of form elements are sent in a form submission, ie input, select, textarea etc. Try this:

echo '<td class="hols" title="' . $resarr[$k]['coms'] . '">';
echo '<form id="showbooking" action="calendar.php" method="post">';
echo '<input type="text" name="clickedcalid" value="' . $resarr[$k]['calid'] . '" />';
echo $resarr[$k]['typ'] . '</form></td>';

Upvotes: 3

Related Questions