gpt4 book ai didi

java - 如何在gwt中使用php

转载 作者:行者123 更新时间:2023-12-02 04:49:49 25 4
gpt4 key购买 nike

正如标题所示

首先,如果这是一个愚蠢的问题,我想道歉=[

所以基本上我正在尝试通过 gwt 发送电子邮件。我不知道 gwt mail 是如何工作的,所以我尝试使用 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,而且当我按下按钮时,它只打印出此代码而不运行它。

这种情况发生在任何人身上吗?有人可以帮助我吗?

谢谢=]

最佳答案

你可以做什么:

  • 使用 FormPanel 在 GWT 中重新创建电子邮件表单,或者
  • 使用 RequestBuilder 在 GWT 中创建您自己的 POST 请求

在这两种情况下:您的 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 代码中。

关于java - 如何在gwt中使用php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29330450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com