development

자바 스크립트를 사용하여 양식을 제출하는 방법은 무엇입니까?

big-blog 2020. 6. 1. 08:20
반응형

자바 스크립트를 사용하여 양식을 제출하는 방법은 무엇입니까?


theForm내부에 제출 버튼이있는 다음과 같은 div 가있는 양식이 있습니다.

<div id="placeOrder" 
     style="text-align: right; width: 100%; background-color: white;">
     <button type="submit" 
             class='input_submit' 
             style="margin-right: 15px;" 
             onClick="placeOrder()">Place Order
     </button>
</div>

클릭하면 함수 placeOrder()가 호출됩니다. 이 함수는 위의 div의 innerHTML을 "processing ..."으로 변경합니다 (따라서 제출 버튼이 사라짐).

위의 코드는 작동하지만 문제는 양식을 제출할 수 없다는 것입니다! 이것을 placeOrder()함수 에 넣으려고했습니다 .

document.theForm.submit();

그러나 그것은 작동하지 않습니다.

양식을 제출하려면 어떻게해야합니까?


name양식 속성을로 설정하면 "theForm"코드가 작동합니다.


당신이 사용할 수있는...

document.getElementById('theForm').submit();

...을 교체 하지 마십시오innerHTML . 당신은 양식을 숨기고 다음 삽입 할 수 처리 ... span 것이다 표시 그 자리에 있습니다.

var form = document.getElementById('theForm');

form.style.display = 'none';

var processing = document.createElement('span');

processing.appendChild(document.createTextNode('processing ...'));

form.parentNode.insertBefore(processing, form);

내 경우에는 완벽하게 작동합니다.

document.getElementById("form1").submit();

또한 다음과 같은 기능으로 사용할 수 있습니다.

function formSubmit()
{
     document.getElementById("form1").submit();
} 

document.forms["name of your form"].submit();

또는

document.getElementById("form id").submit();

이 중 하나를 시도해 볼 수 있습니다.


오래된 게시물이지만 양식 name안에 태그 를 사용하지 않고 양식을 제출하는 방식을 그대로 둡니다 .

HTML

<button type="submit" onClick="placeOrder(this.form)">Place Order</button>

JS

function placeOrder(form){
    form.submit();
}

이 솔루션이 다른 사람을 도울 수 있기를 바랍니다. TQ


//Take form name as "theForm"
//Example:   
         ## ----Html code----- ##    
      <form method="post" action="" name="theForm">
            <button onclick="placeOrder()">Place Order</button>
      </form>

            ## -------javascript code------ ##

        function placeOrder(){
        document.theForm.action="yourUrl";
        document.theForm.submit();
        }

양식에 id가 없지만 theForm과 같은 클래스 이름이있는 경우 아래 명령문을 사용하여 제출할 수 있습니다.

document.getElementsByClassName("theForm")[0].submit();

웹 사이트에 숨겨진 간단한 양식을 사용하여 사용자가 로그인 한 것과 동일한 정보를 사용하여 쉽게 해결할 수있었습니다. 예이 양식에 사용자를 로그인하려면 아래 양식에 이와 같은 것을 추가 할 수 있습니다.

<input type="checkbox" name="autologin" id="autologin" />

as far I know i am the first to hide a form and submit it via clicking a link. There is the link submitting a hidden form with the information. Not 100% safe if you don't like auto login methods on your website with passwords sitting on a hidden form password text area......

Okay so here is the work. Lets say $siteid is the account and $sitepw is password.

First make the form in your php script if you dont like html in it use minimal data then echo in the value in a hidden form. I just use a php value and echo in anywhere i want pref next to the form button as you cant see it.

PHP Form to print

$hidden_forum = '
<form id="alt_forum_login" action="./forum/ucp.php?mode=login" method="post" style="display:none;">
    <input type="text" name="username" id="username" value="'.strtolower($siteid).'" title="Username" />
    <input type="password" name="password" id="password" value="'.$sitepw.'" title="Password" />
</form>';

PHP and Link to submit form

<?php print $hidden_forum; ?>
<pre><a href="#forum" onClick="javascript: document.getElementById('alt_forum_login').submit();">Forum</a></pre>

you can use below code to submit the form using Javascript;

document.getElementById('FormID').submit();

<html>
<body>

<p>Enter some text in the fields below, then press the "Submit form" button to submit the form.</p>

<form id="myForm" action="/action_page.php">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br><br>
  <input type="button" onclick="myFunction()" value="Submit form">
</form>

<script>
function myFunction() {
    document.getElementById("myForm").submit();
}
</script>

</body>
</html>

참고URL : https://stackoverflow.com/questions/9855656/how-to-submit-a-form-using-javascript

반응형