Nick Nick - 6 months ago 23
PHP Question

PHP JavaScript uncaught error with onchange

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!

Answer

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>";
}
?>

Comments