简体   繁体   中英

Clearing input data on submit after opening it in a new tab

Anyways, straight to the point. I am in need of some help on getting my form submit button to clear the input field as well as send the information that was typed. Not very detailed, right?

Here's an example. Person types in field. Person clicks submit. Submitted information is then opened in a new tab. The original tab with the input field is then refreshed to remove the text in the input field.

This is my current form. I tried implemented javascript. It worked for the removing the information on click, but didn't send the information to the handler. Can you tell me how to fix this?

<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">

  <input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
  <button type="submit" class="btn btn-default" onclick="document.getElementById('banfix').value='';return false;">Search</button>

</form>

I don't mind it being in php or javascript. I just need to know how to fix my problem.

Using jquery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
  $(function(){
        //clear data  
        $('.form-inline').on('submit','.form-inline',function(){   
            var value = $("#banfix").val();//now you have the value
            $("input[type=text], textarea").val("");          
            });  
    });         
</script>

提交后可以重置您的表格

$(".myform")[0].reset();

Use onsubmit() , it will work when the user click the button or just press enter

<form class="form-inline" role="form" method="post" target="_blank" action="derp.php" onsubmit="document.getElementById('banfix').value='';">
    <input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
    <button type="submit" class="btn btn-default">Search</button>
</form>

You can do the opening new window in derp.php file after submitting the form.

<?php
        $banfix = $_POST['banfix'];
        $new_url = "YOUR_URL_HERE?fix=".$banfix;
        echo "<script type='text/javascript'>window.open('".$new_url."','_blank');</script>";
        http_redirect('YOUR_FORM_URL_HERE');
        die();
?>
<form class="form-inline" role="form" method="post" target="_blank" action="derp.php">
<input id="banfix" type="text" name="player" class="form-control" placeholder="Player Username">
<button type="submit" class="btn btn-default" onclick="setTimeout(function() {
document.getElementById('banfix').value='';return false;
}, 100);">Search</button>
</form>

I added a set timeout function to my onclick. My problem was that the information being sent was being cleared before it was sent. So I set a 100 tick timeout so the info can be sent first. :3

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