如何使用HTML表单简单地将javascript数据发布到PHP [英] How to simply post javascript data to PHP with HTML form

查看:66
本文介绍了如何使用HTML表单简单地将javascript数据发布到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 javascript 代码中有此代码:

var data = signaturePad.toDataURL('image/png');
document.write(data);

输出看起来更苗条:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZAAAADICAYAAADGFbfiAAAHFklEQVR4Xu3VsQ0AAAjDMPr/0/yQ2exdLKTsHAECBAgQCAILGxMCBAgQIHAC4gkIECBAIAkISGIzIkCAAAEB8QMECB...

而不是document.write(data);,我想通过使用带有隐藏文件的简单HTML表单将数据发布到自身页面上,该表单可以保存JS中的数据.然后,我想使用PHP处理$_POST并将数据存储在变量中...

Instead of document.write(data); I want to post the data to the self page by using a simple HTML form with hidden filed that can hold the data from JS. then, I want to use PHP to handle $_POST and store the data in a variable...

<?php 
// check for form submission...
if(isset($_POST['send'])) {
  // get the data
  $data = $_POST['data'];
}
?>

HTML表单:

<form method="post">
  <input type="hidden" name="data" value="">
  <input type="submit" name="send" value="submit">
</form>

我了解我可以使用AJAX进行此操作,但是我无法弄清楚该表格,我应该如何完成?

I understand that I can somehow do it with AJAX but I couldn't figure out with the form, how should I get this done?

我猜想我正在寻找2种解决方案-首先,如何使用JS表单将数据存储在隐藏字段和后记中,也许如果不能仅仅通过AJAX将其发布到PHP,发布到同一页面...

I guess that I'm looking for 2 solutions - first, how to use the form with JS to store the data in the hidden field and afterwords, maybe how to post it via AJAX to PHP if it can't be just posted to the same page...

推荐答案

有很多方法可以实现此目的.关于您的询问方式,带有隐藏的表单元素.

There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.

将表单更改为:

<form method="post" name="myform" method="post" action="script.php">
  <input type="hidden" name="data" value="">
  <input type="submit" name="send" value="submit">
</form>

JavaScript:

Javascript:

var data = signaturePad.toDataURL('image/png');
document.myform.data.value = data;
document.forms["myform"].submit();

或Jquery:

var data = signaturePad.toDataURL('image/png');
$('form input[name="data"]').val(data);
$("form").submit();

这篇关于如何使用HTML表单简单地将javascript数据发布到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆