2

I have two forms in my page. I hide the form 2 using HTML inline style.

        <form id="productionForm" name="productionForm" method="POST" style="display:none;">

I have input button on form 1.

    <input id="buttonProductionSummary"  class="buttonProductionSummary" type="submit" value="Submit" />

I have JQuery code to load the form 2 on button click of form 1. My JQuery code is as follows.

    <script type="text/javascript">
    $(document).ready(function(){

        $("#buttonProductionSummary").click(function() {
            $("#productionForm").show();
        });
    });
</script>

When i click the button in the form one, the page get reloaded again, so the form 2 appears and disappers again. How to can i make the form 2 to appear when i click button on form 1.

3
  • Does it have to be a submit button at all? I suppose if you want Enter to trigger it then that makes sense, but FYI another solution is to use <input type="button"> which will not submit the form. Commented Dec 15, 2012 at 0:34
  • Form1 contains filters. I need to pass the filter values selected from form1 to mysql database and retrieve values to be filled in form2. If there is no data available for the passed filters, an empty form should be dislplayed. to perform this i need to have submit in form1. Commented Dec 15, 2012 at 3:27
  • You can't submit a form and keep the same web page without reloading unless you use ajax. Commented Dec 16, 2012 at 18:18

4 Answers 4

3

You need to prevent the default behavior of the form:

$("#buttonProductionSummary").click(function(e) {
    $("#productionForm").show();

    e.preventDefault();
});
Sign up to request clarification or add additional context in comments.

Comments

2

as per my requirement i tried to display the form which is to be edit and hide all remaining forms using the following way;

<html>

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

    $("#what").click(function() { //event called

         $(".hello").hide(); // to hide all forms
          $('#ayyappa1').show();  //to dispaly needed form only
          return false //option to stop
 });

 });


</script>


</head>
<body>
<form id ="ayyappa1 " class ="hello"> // declare class for every form
<input type="check" class="what">   // trigger of click event 
</form>
<form id ="ayyappa2 " class ="hello">
<input type="check" class="what">
</form>
<form id ="ayyappa3 " class ="hello">
<input type="check" class="what">
</form>
<form id ="ayyappa4 " class ="hello">
<input type="check" class="what">
</form>
</body>
</html>

Comments

1

The problem is that clicking the button in form 1 is triggering a submission of the form (default event)... Hence, the page reloading. You should prevent that by using the submit event as your trigger, handle the form using AJAX and output the result to #productionForm before displaying:

$("#form1").submit(function() {
    /* AJAX calls and insertion into #productionForm */
    $("#productionForm").show();
    return false;
});

1 Comment

Form1 contains filters. I need to pass the filter values selected from form1 to mysql database and retrieve values to be filled in form2. If there is no data available for the passed filters, an empty form should be dislplayed. to perform this i need to have submit in form1.
1

None of the answers above works, so I figured it out myself. This code works like a charm.

<button id="btn" class="editbutton" >Edit your Profile</button>
<form id="editForm"  action="" method="post" name="editForm">

<input type="text" name="txtname" placeholder="enter your name">

</form>`

<script type="text/javascript">

    $(document).ready(function(){
        $("#editForm").hide();
        $("#btn").click(function(e) {
            $("#editForm").show();
            $("#btn").hide();

        });
    });
</script>

Comments

Your Answer

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.