猿问

POST请求空白,为什么?

我将以下代码另存为send.php:


<?php

$to_email = "person@gmail.com";

$subject = $_POST['subject'];

$body = "Hi, This is test email send by PHP Script";

$headers = "From: Me@myWebsite.com";


if ( mail($to_email, $subject, $body, $headers)) {

    echo("Email successfully sent to $to_email...");

} else {

    echo("Email sending failed...");

}

?>

如果我导航到https://myWebsite.com/Stuff/send.php 并发送电子邮件,该电子邮件将发送$subject = "something"


但是如果我设置$subject为POST变量。当我在浏览器中写入此URL时,它不会发送主题(例如,电子邮件仍然发送,但主题现在为空):


 https://myWebsite.com/Stuff/send.php?subject=notworking


森栏
浏览 302回答 3
3回答

梦里花落0921

要获得两者$_POST ,$_GET您可以使用$_REQUEST适用于两者的东西。在您的代码中替换$subject = $_GET['subject'];和$subject = $_REQUEST['subject'];如果有人直接在浏览器中直接点击了网址,但没有设置主题,则可以阻止他们使用if(!empty($_REQUEST['subject'])){&nbsp; $to_email = "person@gmail.com";&nbsp; $subject = $_REQUEST['subject'];&nbsp; $body = "Hi, This is test email send by PHP Script";&nbsp; $headers = "From: Me@myWebsite.com";&nbsp; if ( mail($to_email, $subject, $body, $headers))&nbsp; &nbsp; echo("Email successfully sent to $to_email...");&nbsp; else&nbsp; &nbsp; echo("Email sending failed...");}

杨__羊羊

尝试一下。&nbsp;<?php&nbsp; &nbsp; $to_email = "person@gmail.com";&nbsp; &nbsp; $subject = (isset($_GET['subject']) && (!empty($_GET['subject']))?$_GET['subject']:"No Subject";&nbsp; &nbsp; $body = "Hi, This is test email send by PHP Script";&nbsp; &nbsp; $headers = "From: Me@myWebsite.com";&nbsp; &nbsp; if ( mail($to_email, $subject, $body, $headers))&nbsp; &nbsp; &nbsp; &nbsp; echo("Email successfully sent to $to_email...");&nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; echo("Email sending failed...");?>

子衿沉夜

如果您要从网址参数中提取值,则需要使用$_GET 全局变量来获取这样的数据,<?php$to_email = "person@gmail.com";$subject = $_GET['subject'];$body = "Hi, This is test email send by PHP Script";$headers = "From: Me@myWebsite.com";if ( mail($to_email, $subject, $body, $headers)) {&nbsp; &nbsp; echo("Email successfully sent to $to_email...");} else {&nbsp; &nbsp; echo("Email sending failed...");}?>
随时随地看视频慕课网APP
我要回答