微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerPoint VBA是否可以发送POST请求?

如何解决PowerPoint VBA是否可以发送POST请求?

我想将在PowerPoint教育游戏中获得的分数发送到在线MysqL数据库中。

我的问题是,这可能吗?

我可以看到使用Excel VBA时似乎有可能,但是我很高兴知道可以使用PowerPoint VBA来完成此操作,如果可以的话,我应该如何处理呢?

谢谢您的任何评论

编辑-我想我将在这里进行更新,因为与注释框相比,可以很好地设置代码。我已经对以下代码进行了试验,发现可以将其发布到数据库中,但是问题是数据库中仅插入了空白值。

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

URL = "http:mydomain/AddNew.PHP?"
objHTTP.Open "POST",URL,False
objHTTP.setRequestHeader "User-Agent","Mozilla / 4.0 ( compatible; MSIE 6.0; Windows NT 5.0); "
    objHTTP.send "name=test,&score=50"

对于将用于插入数据库名称和分数的正确语法的任何建议,我将不胜感激。另外,如何使用变量发送数据?

非常感谢您的任何评论

编辑-评论者说,我需要包括我的PHP代码(在AddNew.PHP中使用)。所以我已经在下面复制了这个。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<Meta content="en-gb" http-equiv="Content-Language" />
<Meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

<?PHP


$name=$_POST['name'];
$score=$_POST['score'];
echo '$name is '.$name;
echo '$score is '.$score;

if( $_REQUEST["name"] || $_REQUEST["score"] ) {
  echo "name ". $_REQUEST['name']. "<br />";
  $name = $_REQUEST['name'];
  echo "Your score ". $_REQUEST['score']. " points.";
  $score = $_REQUEST['score'];
  //exit();
}


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_POST['name'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
  echo $name;
}
$score = $_POST['score'];
  if (empty($score)) {
  echo "score is empty";
  } else {
  echo $score;
  }
}

$servername = "my server name";
$username = "my username";
$password = "my password";
$dbname = "my database name";


try {
$conn = new PDO("MysqL:host=$servername;dbname=mydatabasename",$username,$password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch(PDOException $e) {
echo "Connection Failed: " . $e->getMessage();
}

try {
$conn = new PDO("MysqL:host=$servername;dbname=mydatabasename",PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO mytablename (id,name,score)
VALUES ('1','$name','$score')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}


 $conn = null;
?>

</body>
</html>

编辑-另一个编辑!以下是objHTTP.ResponseText的结果。从底部看第三行,您会发现我的AddNew.PHP没有收到任何有关名称和分数的信息!

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<Meta content="en-gb" http-equiv="Content-Language" />
<Meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>

$name is $score is Name is emptyscore is emptyConnected successfullyNew record created successfully
</body>
</html>

解决方法

好消息-我找到了一个解决方案-我需要更改setRequestHeader,我相信它指定了我正在使用表单请求,并且我还单独研究了如何发送变量。下面的代码应如下所示:

'below are some dummy variables to test and send
Dim name As String
Dim score As Integer
name = "John Doe"
score = 43

Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")

URL = "my URL/AddNew.php?"
objHTTP.Open "POST",URL,False
'objHTTP.setRequestHeader "User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" 'I do not seem to need this line!
objHTTP.setRequestHeader "Content-type","application/x-www-form-urlencoded"

objHTTP.send ("name=" + name + "&score=" + CStr(score))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。