0

I have dropdown list and when somebody chooses something I need to submit it, here's my code:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
   <body bgcolor="#E2E2E2">
       <form action='index.php' method='post' enctype='multipart/form-data'    id='myform'>
     <div div style="position: absolute;left: 35%; top: 30%;">
    <link rel="stylesheet" type="text/css" href="CSS.css">
      <?php
     include ("config.php");
     // Create connection
  //form start

     if ($result->num_rows > 0) {


 // output data of each row
 echo"<tr><td>формат: ";

       echo"<select class='format' name='formater'onchange='document.getElementById('myform').submit()'>";
        while($row = $result->fetch_assoc()) {
      echo "<option value='". $row["$formatPrice"]. "'>".$row["$formatColumn"]. "</option>";
 }

  }
     if(isset($_POST['submit'])){
     echo "aaa";
  }
 ?>

When i choose option that error comes out: Uncaught TypeError: object is not a function onchange Thanks for any help.

Here is rendered html:

    <html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head><body bgcolor="#E2E2E2">
    <form action="client.php" method="post" enctype="multipart/form-data" id="myform">
     <div div="" style="position: absolute;left: 35%; top: 30%;">
   <link rel="stylesheet" type="text/css" href="CSS.css">
    <table border="1px" class="table"><tbody><tr><td>Calculator </td></tr>        <tr><td>ფორმატი: <select class="format" name="formater" onchange="document.getElementById(" myform').submit()'=""><option   value="0.15">A3</option><option value="0.1">A4</option><option value="0.05">A5</option><option value="0.035">A4/3</option>aaa</select></td>        </tr><tr><td><input type="submit" name="submit" class="btn" value="Submit"></td> </tr>


  </tbody></table></div></form></body></html>

So after marking this question as duplicate, Quentin showed me this link "Submit is not a function" error in JavaScript and yes, its absolutely another thing, these answers didn't help me, I think its more in PHP than in js, because i tried this code without PHP and it worked... Now I am becoming more confused with this!

Community
  • 1
  • 1
Nick
  • 455
  • 9
  • 28
  • Might not be the cause of your error, but you are not closing your select tag, does closing it solve your problem? – Stijn Mar 11 '16 at 14:27
  • @Stijn actually i close it, its small part of my code, and i dont think that can be problem anywhere – Nick Mar 11 '16 at 14:30
  • Could you show us the generated output instead of the php? – Stijn Mar 11 '16 at 14:33
  • @Stijn ah ok. if(isset($_POST['submit'])){ echo "aaa"; } – Nick Mar 11 '16 at 14:35
  • I can't reproduce your problem, the form gets submitted when using onchange='this.form.submit()', which browser are you using and are there are no other elements on the page which are causing this problem? – Stijn Mar 11 '16 at 14:48
  • As Stijn suggested, since this is a client-side error, it would be helpful to see the _rendered_ HTML – Patrick Q Mar 11 '16 at 14:53
  • I use google chrome, but I have another submit button in the end, i don't think it can cause any problems – Nick Mar 11 '16 at 14:54
  • As suggested by others, show us the HTML that this PHP outputs. – Taff Mar 11 '16 at 14:58
  • 1
    onchange='document.getElementById('myform').submit()' will not work btw, because the onchange attribute will be closed after: getElementById(, mix the single and double quotes and use \ to escape: echo " – Stijn Mar 11 '16 at 15:01
  • @Stijn yes i added rendered code – Nick Mar 11 '16 at 15:06
  • 1
    Your quotes are a mess, as mentioned in my comment above, make sure all the attributes open and close at the correct place. Mix double quotes and single quotes in your HTML / javascript. I usually use double quotes in PHP, escape double quotes with a slash in HTML and use single quotes in javascript: echo ""; – Stijn Mar 11 '16 at 15:24

1 Answers1

0

I would give the form an ID to make it easier to identify it

<form action='index.php' method='post' id='myform' enctype='multipart/form-data'>

then use this for your onchange event

onchange='document.getElementById("myform").submit()'

I'm not sure if there is something with your formatting. Try...

<form action="index.php" method="post" id="myform" enctype="multipart/form-data">
<?php
echo "<select class=\"format\" name=\"formater\" onchange=\"document.getElementById('myform').submit()\">";
while($row = $result->fetch_assoc()) {
    echo "<option value=\"" . $row['$formatPrice'] . "\">" . $row['$formatColumn'] . "\"</option>";
}
?>

Taff
  • 231
  • 1
  • 13