2013-06-20 4 views
1

사용자 이름과 비밀번호가있는 HTML 양식이 있습니다. 제출 버튼을 클릭하면 두 개의 서로 다른 서버에서 두 가지 다른 PHP 메소드에 게시하려고합니다. 어떻게 할 수 있습니까? HTML 폼을 사용하여이 작업을 수행 할 수 있습니까? 아니면 자바 스크립트를 사용해야합니까? 어떤 제안?단일 양식에서 다른 두 가지 방법으로 게시하십시오.

UPDATE는 다음 코드를 시도했지만 작동하지 않습니다. 내가 여기서 뭐라구? 미리 감사드립니다 ..!

<!DOCTYPE html><!--HTML5 doctype--> 
<html> 
    <head> 
     <title id="login_form">Login Form testing</title> 
     <script type="text/javascript" src="//code.jquery.com/jquery-1.7.2.min.js"></script> 
     <script type="text/javascript"> 

     $('#loginform').submit(function(){ 
       $("form").on("submit", function(event) { 
     event.preventDefault(); // prevent the default submission behavior 
     var data = $(this).serialize(); // gather data from the form and serialize it 
     $.post('URL_1', data, successAction1); // post it to destination 1 
     $.post('URL_2', data, successAction2); // post it to destination 2 
}); 

var successAction1 = function() { alert("Success one");}; 
var successAction2 = function() { alert("Success two");}; 


}); 
     </script> 
     </head> 
     <body> 
     <form id="loginform" action=""> 
<fieldset > 
<legend>Login</legend> 
<input type='hidden' name='submitted' id='submitted' value='1'/> 

<label for='username' >UserName*:</label> 
<input type='text' name='username' id='username' maxlength="50" /> 

<label for='password' >Password*:</label> 
<input type='password' name='password' id='password' maxlength="50" /> 

<input type='submit' name='Submit' value='Submit' /> 

</fieldset> 
</form> 
     </body> 
+1

왜 그렇게하고 싶습니까? –

+0

왜 1 대의 서버에 게시하고 다른 곳으로 가야 할 데이터의 부분을 보내야합니까? –

+0

@EliasVanOotegem 그의 호스트가 서버 측 스크립팅을 허용하지 않을 수 있습니까? – Mr47

답변

3

예를 들어 AJAX를 사용하여 게시 할 수 있습니다. JQuery 라이브러리 :

$("form").on("submit", function(event) { // listen to form submission (use whatever selector suits you, but make sure it is the form itself) 
     event.preventDefault(); // prevent the default submission behavior 
     var data = $(this).serialize(); // gather data from the form and serialize it 
     $.post('URL_1', data, successAction1); // post it to destination 1 
     $.post('URL_2', data, successAction2); // post it to destination 2 
}); 

var successAction1 = function() { /*what to do after 1st post*/}; 
var successAction2 = function() { /*what to do after 2nd post*/}; 

나는 약 $.serialize$.post

+0

감사합니다. 나는 그것을 작동 시켰어 :) – harsh

1

JavaScript onclick 함수를 사용하고 두 함수를 호출하십시오. 아약스 사용

2

그래,이 작업을 수행 할 수있는 API 문서를 읽는 게 좋을 것,하지만 당신은 몇 가지 자바 스크립트를 필요로하기 위하여려고하고있다.

<form id="#login"> 
    <p> 
    <label> 
    Username: 
    <input type="text" name="username" /> 
    </label> 
    </p> 
    <p> 
    <label> 
     Password: 
     <input type="password" name="password" /> 
    </label> 
    </p> 
    <p> 
    <button type="submit">Login</button> 
    </p> 
</form> 

다음이 자바 스크립트 당신이 시작됩니다 : 당신은이 솔루션이 작동하려면 페이지에 설치 jQuery를해야합니다

$('#login').on('submit', function(e) { 
    e.preventDefault(); // stops the normal functionality of the form 
    $.ajax({ 
    url: 'http://website1.com', 
    data: $(this).serialize(), 
    success: (data) { 
     // do whatever 
    } 
    }); 
    $.ajax({ 
    url: 'http://website2.com', 
    data: $(this).serialize(), 
    success: (data) { 
     // do whatever 
    } 
    }); 
}); 

이 가정

는 형태입니다.

검색어 ajax에 대한 자세한 내용은 http://api.jquery.com/jQuery.ajax/을 참조하십시오.

다른 도메인 이름으로 다른 웹 사이트에 실제로 게시하는 경우 교차 출처 문제가 발생합니다. 해결책은 CORS를 사용하는 것입니다. 당신은 stackoverflow 많은 솔루션을 찾을 수 있습니다.

관련 문제