0

I have two forms here, the first is a select dropdown select, which when posted generates another form, creating an amount input boxes based on the number selected. This second form then puts the data from the input boxes into 2 array. All this works perfectly but I am trying to add extra functionality to the submit button on the second form. I created a function 'showcontet()' to show and hide the content in the div with id 'table'. This works perfectly when used on a button outside of the form. When used on the submit button inside the form you can see the content for about 1 second before it then disappears again.

    <head>
      <script type="text/javascript">
           showcontent = function(){
              var content = document.getElementById('tabl');
              content.style.display = 'inline';
           }

      </script>
        <?php 
    $userkey = array();
    $userval = array();
    $usernum = $_POST['usernum'];
    $total = 0;
    ?>


        <form action='MYPIE.PHP' method='POST'>
                HOW MANY SECTIONS?
                    <select name="usernum" value="<?php echo $usernum;?>">
                          <option>1</option>
                          <option>2</option>
                          <option>3</option>
                          <option>4</option>
                          <option>5</option>
                          <option>6</option>
                          <option>7</option>
                          <option>8</option>
                          <option>9</option>
                    </select>
                <input type="submit" name="submitnum" value="submit selection" />
        </form>


    <form action='MYPIE.PHP' method='POST'>     
        <?php 
            for ($i=1; $i<$usernum+1; $i++){
                echo '<input type="hidden" value="' .$usernum.'" name="usernum" />';
                echo "<br>insert key:   <input name='key".$i."' value='".$userkey[$i]."'>   insert value:   <input name='val".$i."' >";

            $userkey[$i] = $_POST["key".$i];
            $userval[$i] = $_POST["val".$i];
            }

        ?>
        </br>
        <input type="submit" value="submit" name="submit keys" onclick="showcontent()" />
    </form>

    </table>


    <div id="tabl" style="display:none;">
    content
    </div>    

Apologies for the bad indentation, I am pretty new to this & thanks for any help

2
  • Sorry if the explanation didn't make sense. I am not trying to stop the form submitting, I am trying to get it to submit the data and run the showcontent() at the same time Commented Jul 6, 2012 at 12:50
  • If you submit the form it will send a server request, so the page MYPIE.PHP will load. If you want to run showcontent() to change some div content and also submit the form, you should use ajax. Commented Jul 6, 2012 at 13:00

3 Answers 3

2

You need to return false from the event handler to prevent the browser from submitting the form and loading the page.

Sign up to request clarification or add additional context in comments.

1 Comment

Probably better if you use an e.preventDefault(). fuelyourcoding.com/jquery-events-stop-misusing-return-false
0

make your second button not a 'submit' button but a regular 'button' else it will fire the submit of the form...

or

set the 'showcontent()' function on the forms 'onsubmit' event and let that function return false:

onsubmit="return showcontent()"

function showcontent(){
    var content = document.getElementById('tabl');
    content.style.display = 'inline';

    $.ajax({
        type: "POST",
        url: "MYPIE.PHP",
        data: { usernum: $("select[name='usernum'] option:selected").val() }
        }).done(function() {
            alert('data posted');
    });

    return false;
}

6 Comments

thanks! I tried onsubmit but it didn't do anything, changed that to onclick and everything works perfect thanks!
bit premature there. This stops the form submitting at all, I am needing it to submit the form data and run showcontent()
then what's the use of showing that div, if you actually want to post the form and make a new request to the server? removing the 'return false;' should be enough then...
The div actually contains a table that is filled from the arrays that are populated by the form when submitted. I left this out the code to keep it simpler. Some parts of the table are already generated before form so thats why I have it hidden. So basically I wanted the submit button to populate the arrays and show the table (the div) when clicked. Taking out 'return false' just goes back to as before, when the div shows up for a second then dissapears
that's what happens when you submit the form: the data is posted and the HTML is reloaded. So that's why the div will only show up for a second... If you do not want the page to reload, you should post the data using AJAX, that way you can show the div and have the data processed in the background.
|
0

You need add e.preventDefault(); to the event handler to prevent the browser from submitting the form and loading the page.

Don´t use return false as this is other sideeffects you might not want. see http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/

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.