-2

I am C++ Programmer and new to JS World.

This refers to my previous question.

How to submit form based on certain condition?

I am trying to submit my form using document.getElementById based on certain condition but there is no output.

<!doctype html>
<html lang="en">
<head>
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<title>jQuery.post demo</title>
<body>
<meta charset="utf-8">
<div id="form-container" style="padding-top: 10px; padding-bottom: 10px;">

<script>
  var x=Math.random();;
  if(x > 0.5)
  {
      //document.writeln(x);
      document.getElementById('form-container').submit();
  }  
</script>
X Value: <input type="text" name="X"><br>
<br>
<p style="text-align: left;"><button type="button" id="submit-button">Submit</button></p>
</div>
</body>
<script>
$(document).ready(function(e) {
  $('#submit-button').click(function(event){
    $('#form-container').submit();
  )};
)};
</script>
</html>
Community
  • 1
  • 1
Manish
  • 3,341
  • 15
  • 52
  • 87

3 Answers3

3

You can't do a .submit() on a <div>! You need a <form> Tag with an Action and an method Attribute.

The action Atrribute tells the browser where he has to send the data and the method Attribute says on what way (POST or GET) to send the Data.

<form Action="process.php" method="POST">

Of course you can add an ID too, for the identification of the form.

j_s_stack
  • 667
  • 1
  • 5
  • 18
  • But i have one code part of my project where i have div element on the page which has submit button on clicking submitting button it generates some plots using data filled in the form. – Manish Jan 26 '15 at 09:18
0

You should use a form in your HTML and specify properties to allow form submission:

<form id="form-container" action="url to your action where the form submit request will hit" method="get">

Ref : http://www.tutorialspoint.com/html/html_form_tag.htm

Vijay
  • 2,965
  • 1
  • 15
  • 24
  • In my case there is no action but Jquery is attached with button which generates plot. – Manish Jan 26 '15 at 09:25
  • One example is like button call Jquery function $.ajax({ type: "POST", url: "some.php", data: { name: "John", location: "Boston" } }) .done(function( msg ) { alert( "Data Saved: " + msg ); }); – Manish Jan 26 '15 at 11:02
0

you should submit form not div so include url and method type in form tag