如何在 gwt 中使用 php

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,而且当我按下我的按钮时,它只打印出这段代码而不是 运行。

有人遇到过这种情况吗?有人可以帮助我吗?

谢谢=]

你可以做什么:

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

在这两种情况下:'echo' 声明您的 PHP 表单 returns 将 不会 附加到 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
  }
} );

创建您自己的 POST-RequestBuilder 请求:

// 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 代码中。