[英]how to use php in gwt
就像標題上所說的
首先,如果這是一個愚蠢的問題,我想表示歉意。
所以基本上我想通過gwt發送電子郵件。 我不知道gwt郵件是如何工作的,所以我嘗試使用php方式(對我來說更熟悉),但是我不知道如何使它工作。
所以..在我的war文件夾中,有我創建的index.html和email.php。 在index.html中,有一個調用我的email.php的表單。
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "did you make sure to fill everything?";
return false;
}
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
// Create the email and send the message
$to = 'myemail@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email\n\n Message:\n$message";
$headers = "From: noreply@somedomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
上面是我當前正在使用的PHP代碼。 但是,不僅eclipse無法識別php,而且當我按下我的按鈕時,它只會打印出此代碼而不運行它。
這會發生在任何人身上,誰能幫助我?
謝謝=]
你可以做什么:
在兩種情況下:PHP表單返回的'echo'語句不會附加到HTML頁面,而是以字符串形式返回給您。 您將必須在GWT代碼中決定要做什么(告訴用戶?采取其他措施)。
重新創建為表單面板:
// create the textboxes of the form with their proper form names
TextBox tbName = new TextBox();
tbName.setName( "name" );
TextBox tbEmail = new TextBox();
tbEmail.setName( "email" );
TextBox tbMessage = new TextBox();
tbMessage.setName( "message" );
// create the form panel
final FormPanel emailFormPanel = new FormPanel();
// TODO: add the form panel to some kind of parent widget / ui object
emailFormPanel.setAction( "/contextRoot/path/to/email.php" );
emailFormPanel.setMethod( "POST" );
// add the textboxes to the form panel
emailFormPanel.add( tbName );
emailFormPanel.add( tbEmail );
emailFormPanel.add( tbMessage );
// create the form submit button
Button btnSubmit = new Button( "Submit", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
emailFormPanel.submit();
}
} );
// create the formpanel handler for a successful submit
// any error message ("did you forget to ...") will be returned here
emailFormPanel.addSubmitCompleteHandler( new SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
String errorString = event.getResults();
// TODO: decide what to do with a potential non-empty string
}
} );
使用RequestBuilder創建自己的POST請求:
// create the textboxes of the form with their proper form names
final TextBox tbName = new TextBox();
final TextBox tbEmail = new TextBox();
final TextBox tbMessage = new TextBox();
// create the form submit button
Button btnSubmit = new Button( "Submit", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
submitEmailFormRequestBuilder( tbName, tbEmail, tbMessage );
}
} );
// TODO: add textboxes and Submit-Button to the DOM-tree
提交文本框值:
protected void submitEmailFormRequestBuilder(TextBox name, TextBox email, TextBox message) {
// create the request content in a way that the php script can read it:
// for every textbox the php textbox-name = user-value
StringBuilder requestData = new StringBuilder();
requestData.append( "name=" + name.getValue() );
requestData.append( "&email=" + email.getValue() );
requestData.append( "&message=" + message.getValue() );
// create the REST request callback
RequestCallback callback = new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
String errorMessage = response.getText();
// TODO: handle potential error-message
}
@Override
public void onError(Request request, Throwable exception) {
// TODO: handle timeouts and other sending failures like cross-domain posting
}
};
// create the REST request as POST request
RequestBuilder build = new RequestBuilder( RequestBuilder.POST, "/contextRoot/email.php" );
try {
build.sendRequest( requestData.toString(), callback );
}
catch (RequestException e) {
// handle exception
}
}
否則,如果index.html不是調用.nocache.js模塊的GWT起始頁,則可以將其與IFrame一起包含在GWT代碼中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.