Question

I have a form with 2 selectboxes a textbox and 1 textarea. When i load this form it gets all the menu items and language items from the database and inserts them in the selectboxed and then gets the corresponding content from menu item and language.

 <form style="border: 1px solid black; " name="form" method="post" action="">
      Content selecteren om te wijzigen:
      <table>
        <tr>
          <td width="78">Menu item:</td>
          <td><select name="Menu" onchange="this.form.submit()">' . $this->MenuItems->render() . '</select></td>
        </tr><br>

        <tr>
           <td width="78">Taal:</td>
           <td><select name="Language" onchange="this.form.submit()">' . $this->LanguageItems->render() . '</select></td>
       </tr>

       <tr>
        <td>Menu Item:</td>
        <td width="78"><input name="MenuItemTextBox" type="text" id="MenuItemTextBox" value="' . $_POST['Menu'] . '"></td><br>
       </tr>

       <tr>
         <td>content</td>
         <td><textarea style="" Name="Content" id="NewContent" ROWS=10 COLS=50>' . $this->GetContent->render() . '</textarea></td>
       </tr>

        <tr>
          <td><input type="submit" name="Submit" value="Wijzigen"></td>
        </tr>
    </table>
</form>

When you select a different value from the selectboxed it reloads the form/page and displays the correct content in the textare again with "this.form.submit()", and it also remembers what you have chosen in the selectbox.

Now i want to submit all these posts from this form to a file ChangeContent.php But i cant use the action attribute in the form otherwise it will always go to this file when you select a new value from the selectbox.

so my question in short: Reload form onchange while able to use the action attribute on a button submit press.

Thank you

Was it helpful?

Solution

if I understand the question correctly, you change the url of form via javascript/jquery like so..

<select onchange="submitform('url1')"></select>
<select onchange="submitform('url2')"></select>

<script type="text/javascript">
function submitForm(url)
{
  $("#formid").attr("action",url);
  $("#formid").submit();
}
</script>

and if you do this on submit button, you may try something like so:

<input type="submit" onclick="return submitformviabtn()"></select>
<select onchange="submitform('url2')"></select>

<script type="text/javascript">
function submitformviabtn(e)
{
        e.preventDefault();
  $("#formid").attr("action","page.php");
  $("#formid").submit();
}
</script>

OTHER TIPS

 <script>
  $(document).ready(function(){

  $("#textBox").keyup(function()

     changeTest($(this).val());
     });


    function changeTest(val)
    {  
     rowno=0;
     var str="";
     str = "http://www.xyz.com/index.php/api/getArtist?searchtext="+val;

     $.getJSON(str, function (data) {

        var artistname="";
        var artistid="";
        $.each( data, function(key1,value1) {
        $.each(data[rowno], function(key,value) {                 

            if(key=='artistname')
                    {
              artistname=value ;
                    }
       }); 
      availableTags[rowno] = artistname;
      rowno=rowno+1;
       }); 
     });  
      $( "#textBox" ).autocomplete({
   source: availableTags
  });
     }


  </script>



    <form action="your-page.php" method="post">

    <label>Event Category</label>

    <select name="eventTypeidFk" id="eventTypeidFk" class="" required onchange="changetextbox()">

                <option value="">--- Select Event Category ---</option>
                <option value="1">Sports</option>
                <option value="2">Concert</option>

                </select>           

                <label>Artist</label>
                <input type="text" name="artist1" id="textBox" class="" required>


      </form>

You can use this concept. Create an api to fetch data from db and with onchange call that method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top