2

To learn about web programming, I'm trying a simple html/javascript/php "product search" page. Here is the index.php file:

<html>
 <head>
 <title>Product Search</title>
 <script type="text/javascript">
 function mySubmit() {
 alert("submitting...");
 var product_type = $('product_type').value;
 $.post('product_search.php', { product_type: product_type }, function (data) {
 alert("result = " + data); 
 $('body').html(data);
 }
 return false;
 }
 </script>
 </head>
 <body id="body">
 <script type="text/javascript">
 mySubmit();
 </script>
 <form class="form" id="myform" method="post" onsubmit="javascript:return mySubmit();">
 Product Type:
 <input id="product_type" type="text" name="product_type"/>
 <br/><br/>
 <input id="submit" type="submit" value="Submit"/>
 </form>
 </body>
</html>

and the product_search.php file is very small:

<?php
$product_type = $_POST["product_type"];
echo "<h1>$product_type</h1>";
?>

The problem is that it seems like the onsubmit event is never called for the form. I thought it was a JavaScript problem (as in, not allowed to run), but if I run an alert instead of calling mySubmit() when the onsubmit event is called, it works!

Also, am I wrong in thinking that the JavaScript just under the <body id="body"> should be run? It is never run either.

So, for those who didn't read all that: why is my JavaScript function not being run when the onsubmit event is fired?

asked Jul 12, 2013 at 17:59
11
  • Are you getting errors in the console? I would expect at least a $ is undefined based on your example. Commented Jul 12, 2013 at 18:01
  • Nope, no errors. Why should I get that error? Isn't $ defined by jQuery to be document.getElementById? Commented Jul 12, 2013 at 18:02
  • where is your reference to jquery? Commented Jul 12, 2013 at 18:04
  • What do you mean? Isn't it referenced by default? I'm using xampp, if that helps. Commented Jul 12, 2013 at 18:04
  • nope .. you have to include it .. jquery is a library not available on its own. Commented Jul 12, 2013 at 18:05

2 Answers 2

4

I get the following parsing errors when I run your code:

Uncaught SyntaxError: Unexpected token return
Uncaught ReferenceError: mySubmit is not defined 

You are missing the closing ) on your $.post call, which is breaking the parser. Once you fix that, you'll get the $ is undefined error until you reference jQuery.

http://jsfiddle.net/Z6hJK/

answered Jul 12, 2013 at 18:25
Sign up to request clarification or add additional context in comments.

1 Comment

You were right about the missing ); - I added that, and now calling mySubmit(); works! I didn't get the $ is undefined error, however... where would I see it?
0
<script type="text/javascript">
 mySubmit();
 </script>

above function is called but when it is called, even document.getElementById('product_type').value gives error (which might not be enabled on your browser to catch) as it product_type turn out to be null as javascript in body gets executed before browser has fully rendered the page. The moment it is getting called, product_type is not yet rendered.

On click also your function is getting called, but this time may be it is failing as you do not have reference to Jquery. include one.

answered Jul 12, 2013 at 18:27

2 Comments

"I would be thankful if somebody can explain it".. ah, no, the answer section is not the place to ask a question.
No need ... I have figured it out.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.