Viewed   97 times

I want to change the form action based on a selection value.

<form name="store" id="store" method="post" action="">
<select name="storeID">
<option value="/stores/store6.php">6</option>
<option value="/stores/store10.php">10</option>
</select>                   
</form>

Now I want the form action, to use the select option value. For example:

If Selection 1 is selected, use the folowing form action /stores/store6.php

 Answers

5

You can use the onchange event to change the form's action

document.getElementById('store').storeID.onchange = function() {
    var newaction = this.value;
    document.getElementById('store').action = newaction;
};

Here is a jsfiddle with the code.

Tuesday, December 20, 2022
 
1

You can use the File.name web API for this.

To accomplish this, you'll want to add an onchange event to your input field, and then create a javascript function to capture the event. In that function, you'll access the selected file's name and perform the validation. For example:

<input
    type="file" name="userfile" id="userfile"
    accept="image/jpeg, application/pdf"
    onchange="validateFile(this)"
/>

<script>
    function validateFile(fileInput) {
        var files = fileInput.files;
        if (files.length === 0) {
            return;
        }

        var fileName = files[0].name;
        if (fileName.length > 15) {
            alert('File input name to long.');
        }
    }
</script>

It's worth noting, for security reasons, you should not store the file with the name the user supplies. It's safe to store it in a database and used as a label when showing users (pending you sanitize it first) - but the file path you save on your server should be 100% controlled by you.

Friday, August 19, 2022
 
1

The good news is that PHP and JavaScript have a similar idea about what values are true and false.

  • An empty string will be false on both sides. A string with something in it (except 0 in PHP) will be true on both sides.
  • The number 0 will be false on both sides. All other numbers will be true on both sides.

Since the values of a form will always be strings, as Quentin pointed out in his answer, a good practice might be to use an empty string as false value and something else (e.g. 'true') as true value. But I think your way of using 0 and 1 and testing the numerical values is the safest approach because it isn't misleading. (When someone sees 'true' they might think 'false' would also be usable for a false value.

Saturday, August 20, 2022
4
$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});
Tuesday, November 29, 2022
 
4

Change the line where you set the contents of the paragraph from

priceDesc = "Enter price per month";

to

priceDesc.innerHTML = "Enter price per month";

Currently, you are just changing the priceDesc variable to contain a string instead the paragraph node. Setting the innerHTML attribute of a node changes the html contained inside of it. :D

Monday, October 31, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :