简体   繁体   中英

Submitting form with jQuery and PHP

I'm trying to submit a form when a button on another form is pressed. The form will simply be updated, and I'm trying to capture any information that has already been typed in.

This is just how the information flows. The forms actually aren't identical, this is just for simplification of the concept.

<?

function form(){
    ?>
    <form method="post" id="form1">
    <input type="text" name="textinput"/>
    <input type="submit" name="submit_form1"/>
    </form>

    <?
    $form2 = new forms();  //assume form 2 is setup similarly to form1 and its <form> tags are in that form like they are for form 1
    $forms->get_form2();
    ?>
}

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">

$('#form2').click(function() {
  $('#form1').submit();
});

</script>
<?

I tried doing something like this but it's not working. Assuming form 1 has something entered (but not submitted), I'd like to capture these values when a button on form 2 is pressed.

I also tried this (which I feel would work adequately) but I had no success either.

$('#form1').submit(function() {
  alert('Handler for .submit() called.');
  return false;
});
$(document).ready(function(){
    $('#form2').submit(function(){
        $('#form1').submit();
        return false;
    });
});

You need to wrap it with document.ready() if this is what you want. Though this way, you will only submitted form1, not form2.

Once a form is submitted the server will return a result to the client, however what you are trying to do here is submit 2 forms at the same time (if i understand you correctly) and that simply cannot be done.

if you want to save information of one form before submitting another you will need to either send a request and update the session on the server or keep the information localy with localstorage or cookies and on the new page that loads either get the data from the session or the cooike.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM