-1

I pass data from a form (once hidden and once by input) by click to a function. But unfortunately the data on the hidden input field is given to the $_POST

<?php>
$abschlag1 = "gelb";
$ndbw01 = 8;
echo "<form name='Formular' action='' method='post' >";
echo "<td align=center><input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2'>" . $ndbw01 . "</td>";
echo "<input type='hidden' id='abschlag1' name='abschlag1' value=$abschlag1>";
echo "<input type='submit' name='submit1' value='Speichern' onclick='chkFormular();'/>";
echo "</form>";
?>

<script type="text/javascript">
function chkFormular() {

<?php
if(isset($_POST['submit1'])) {
$abschlag1 = $_POST['abschlag1'];
$L01w = $_POST['L01w'];
}
?>
}
</script>

The result ist:

$_POST['abschlag1‘] => gelb

$_POST['L01w‘] => empty

I hope, it is now a little clearer.

wolli911
  • 7
  • 5
  • Hi there! I noticed that your question might need a bit more context to make it clearer. Can you please provide some additional details so that the community can better understand what you're asking? Thanks! – Rager Feb 11 '23 at 14:20

1 Answers1

-1

You can't run a PHP script on a JavaScript event directly. PHP usually is executed at page generation on the server side but if really needed you can run a PHP script on a JavaScript event using Ajax for example.

In order to help you, correct me if I'm wrong, but I think what you want to do is having your fields still completed with the sent values after the form is submitted. You can do it like this using:

value='<?php if isset($_POST["field"]) echo $_POST["field"]; ?>'

<form name='Formular' action='' method='post'>
    <td align='center'>
        <input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?php if isset($_POST['L01w']) echo $_POST['L01w']; ?>' /><?= $ndbw01; ?>
    </td>
    <input type='hidden' id='abschlag1' name='abschlag1' value='<?php if isset($_POST['abschlag1']) echo $_POST['abschlag1']; ?>' />
    <input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>

If you still want to keep your data in variables, you update your code to this:

<?php
if(isset($_POST['submit1'])) {
    $abschlag1 = $_POST['abschlag1'];
    $L01w = $_POST['L01w'];
}
?>

<form name='Formular' action='' method='post'>
    <td align='center'>
        <input type='text' pattern='\d*' maxlength='2' id='L01w' name='L01w' size='2' value='<?= $L01w; ?>' /><?= $ndbw01; ?>
    </td>
    <input type='hidden' id='abschlag1' name='abschlag1' value='<?= $abschlag1; ?>' />
    <input type='submit' name='submit1' value='Speichern' onclick='chkFormular()' />
</form>