[英]how to store images in database from image tag using PHP?
I have an image tag like this:我有一个像这样的图像标签:
<img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">
where the image source is set dynamically by JavaScript.其中图像源由 JavaScript 动态设置。 I want to store this image in database using PHP if the source is set.
如果设置了源,我想使用 PHP 将此图像存储在数据库中。 This should happen on click of a button.
这应该在单击按钮时发生。 This is my PHP code where I'm just checking if database insert works and it does.
这是我的 PHP 代码,我只是在其中检查数据库插入是否有效并且确实有效。
if(isset($_POST['save'])){
$name="checkcheck";
global $wpdb;
$table = $wpdb->prefix ."authorlist";
$wpdb->insert(
$table,
array(
'authorname' => $name
)
);
}
Now how do I modify this code to store that image?现在如何修改此代码以存储该图像?
EDIT 1: This is what I did after what you guys suggested编辑 1:这是我按照你们的建议所做的
if(isset($_POST['save'])){
$image = $_POST['img_src_value'];
global $wpdb;
$table = $wpdb->prefix ."authorlist";
$wpdb->insert(
$table,
array(
'authorname' => $image,
)
);
}
and this is my image tag:这是我的图片标签:
<img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">
<form method="POST" name="imgForm">
<input name="img_src_value" type="hidden" class="img_src_value" value="">
</form>
although you must know that this image tag is also within a form itself.尽管您必须知道此图像标签也在表单中。 So do I put another form tag?
那么我要放另一个表单标签吗?
Take a form tag and hidden field.取一个表单标签和隐藏字段。 With javascript also add img src value to hidden field.
使用 javascript 还可以将 img src 值添加到隐藏字段。
<img src="" id="img1" alt="img1" width="100" height="100" style="display: none;">
<input name="img_src_value" type="hidden" class="img_src_value" value="">
Then to save然后保存
if(isset($_POST['save'])){
$imgSrc = $_POST['img_src_value']; /// You can get that value here
}
While you set image source dynamically at same time set same value in hidden field and while submitting form you will get it in Request value.当您同时动态设置图像源时,在隐藏字段中设置相同的值,并在提交表单时,您将在请求值中获得它。
You will receive it as您将收到它作为
$name="checkcheck";
$image = $_POST['imagesource'];
global $wpdb;
$table = $wpdb->prefix ."authorlist";
$wpdb->insert(
$table,
array(
'authorname' => $name,
'image' => $image
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.