0

Hi i want send html select option disabled attribute with POST and enabled select option attribute with onclick in input form. For example i have:

<script>
function checked(){
     var sel = document.getElementById("table A");  
    if(sel.options[sel.selectedIndex].text=='AAAA'){
         document.getElementById('table B').disabled = false;
    }
}

</script>


<?php
        print("<form method=\"POST\">");
        <print("<tr><th>Table A</th><td>
        <select id='table A' name='table1'>
                <option value=' '>Select</option>
                <option name='AAAA' value='A' onclick=\"checked()\">AAAA</option>
                <option name='BBBB' value='B'>BBBB</option>
        </select></td></tr>");
     print("<tr><th>Table B</th><td>
        <select id='table B' name='table2' disabled>
                <option value=' '>Select</option>
                <option name='BBB' value='B'>BBBB</option>
                <option name='CCC' value='C'>CCCC</option>
                </select></td></tr>");
        print("<tr><td><input type=\"submit\" id=\"save\" name=\"save\" value=\"Save\" ></td></tr>");

if(isset($_POST['save'])){
if(document.getElementById("table B").disabled){
            return ' ';
            }else{
                $type_modify=$_POST['table B'];
                }
?>

Now if i I do not disable select form table B i want send with post ' '. If i click select option name='AAAA' i want enable select with id='table B'. My code doesn't work. why?

user3783243
  • 5,368
  • 5
  • 22
  • 41
Ely94
  • 81
  • 8
  • Try `onchange` on the `select` instead of `onclick` on the `option`, for starters. Does this answer your question? [Is there an onSelect event or equivalent for HTML – Markus AO Feb 06 '21 at 14:26
  • Also, `if(isset($_POST['save'])){ if(document.getElementById("table B").disabled){` here you're mixing PHP and Javascript. PHP cannot access DOM. – Markus AO Feb 06 '21 at 14:28
  • oh no. i can't mixing PHP and Javascript. And how can i do it? can i dabled end enable input form only with php? – Ely94 Feb 06 '21 at 14:29
  • You can't have JS syntax, functions (like `getElementById`) or objects (like `document`) parsed as PHP and expect them to work. Otherwise, you can of course generate JS with PHP. You should be getting errors with the above, assuming your error reporting is turned on. What you need to do is e.g. have an `onsubmit` javascript that includes additional data, otherwise not submitted in your form, for submission. – Markus AO Feb 06 '21 at 14:33
  • How is `$type_modify` used? The rest of the PHP should just be HTML. You also should stick with one encapsulation method, double quotes for attributes or single, that's a personal preference though. – user3783243 Feb 06 '21 at 14:41
  • `Table A` <-- the first `<` will throw a syntax error. Copy/paste typo? And why are you starting a PHP block ` – M. Eriksson Feb 06 '21 at 14:51

1 Answers1

1

You can do it like this.

For example

<script>

function processForm(e) {
    if (e.preventDefault) e.preventDefault();

    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = "my-disabled-check-input";
    input.value = document.getElementById('table B').disabled;
    this.appendChild(input);

    this.submit();
}

var form = document.getElementById('my-form');
if (form.attachEvent) {
    form.attachEvent("submit", processForm);
} else {
    form.addEventListener("submit", processForm);
}
</script>

You could also just add another onclick function as well without the same event listener and then submit the form with form.submit();

Then in your PHP file just treat it as a normal POST variable. You would want to look at $_POST['my-disabled-check-input']; if we are doing it like I setup.

In your form html you can set the POST URL. This URL can be the page that displays the form and also receive the POST variables. If you are unfamiliar with these terms then have a look at https://www.w3schools.com/html/html_forms.asp

But basically you can set an "action" which is just the URL to the php file.

So taken directly from W3schools a form looks like this.

<form action="/action_page.php">
  <label for="fname">First name:</label><br>
  <input type="text" id="fname" name="fname" value="John"><br>
  <label for="lname">Last name:</label><br>
  <input type="text" id="lname" name="lname" value="Doe"><br><br>
  <input type="submit" value="Submit">
</form>

If wanted to have a PHP file that displayed the form and took $_POST variables we can do something like this.

<?php
if($_POST) {
 echo "Post Variables sent";
 var_dump($_POST);
} else {
 echo "Where I will put my form HTML and JS.";
}
?>

Your PHP can also be used like this to make writing HTML easier.

<?php if($_POST): ?>
    Your name is <?=$_POST['fname']?> <?=$_POST['lname']?>.
<?php else: ?>
    <form action="/action_page.php">
      <label for="fname">First name:</label><br>
      <input type="text" id="fname" name="fname" value="John"><br>
      <label for="lname">Last name:</label><br>
      <input type="text" id="lname" name="lname" value="Doe"><br><br>
      <input type="submit" value="Submit">
    </form>
<?php endif; ?>

This <?=$_POST['fname']?> is equivalent to <?php echo $_POST['fname']; ?> but just a little shorter and I like using it that way in my HTML views.

This script doesn't take into consideration any user input parsing so keep that in mind. You may also want to look into HTML 5 form validation to make sure the input is only information you expect and also PHP filtering of user input.

Goddard
  • 2,863
  • 31
  • 37
  • so i can't mixed php and js in the same page with form tag and method post. For example i can write js in one page and php form in other page. is it right? – Ely94 Feb 06 '21 at 15:30
  • You can, but you will need an if statement to detect if the $_POST variable is set to let you know you should submit to your database. – Goddard Feb 06 '21 at 15:50
  • can you give me an example? I am a beginner and i am understand php and javascript. – Ely94 Feb 06 '21 at 15:55
  • I updated it so you can get a better idea of how it works. – Goddard Feb 06 '21 at 20:08