简体   繁体   English

如何使用 AJAX 和 PHP 同时将一张图片和“两个 textareas 值”发布到数据库?

[英]How can I be able to post one image and 'two textareas values to database simultaneously using AJAX and PHP?

Modification to this script so that I can insert image and texts simultaneously (JSON).修改此脚本,以便我可以同时插入图像和文本 (JSON)。 with jquery.form.js plugin , I want to insert both image and textarea values...Tell me if there could be any other alternatives to do it without using jquery.form.js plug in. I'm new to ajax...please help me .使用 jquery.form.js 插件,我想同时插入图像和 textarea 值......告诉我是否还有其他替代方法可以在不使用 jquery.form.js 插件的情况下做到这一点。我是 ajax 的新手。 。请帮我 。

Image goes to ajaximage.php and texts go to ajtest.php,When I upload image and text simultaneously, image is inserted,then texts are inserted,one submit uses two rows(when textarea and image fields are not empty), 1st row is used for image and 2nd row for text areas , This is my problem , I want to insert image and texts in one row.图片转到ajaximage.php,文本转到ajtest.php,当我同时上传图片和文本时,插入图片,然后插入文本,一次提交使用两行(当textarea和image字段不为空时),第一行是用于图像和文本区域的第二行,这是我的问题,我想在一行中插入图像和文本。

Script :脚本 :

<script type="text/javascript" src="scripts/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.form.js"></script>

<script type="text/javascript">
$(document).ready(function() { 


$('#photoimg').live('change', function(){ 
$("#preview").html('');
$("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();

        });
    }); 

</script>

<script type="text/javascript">
$(document).ready(function() {
$(".submit_button").click(function() {
var message = $('#content').val();
var textcontents = $("#source").val();


$.ajax({

type: "POST",
url: './ajtest.php',
data: {tc:message,ts:textcontents},
cache: true,

});

});
});
</script>

FORM :形式

<form id="imageform" method="post" enctype="multipart/form-data"           action='ajaximage.php'>
Upload your image <input type="file" name="photoimg" id="photoimg" />
</form>


<form id="text" method="post" action=''>
<textarea style="width:728px; font-size:14px; height:60px; font-weight:bold;      resize:none;" name="content" id="content" placeholder="Thread content" ></textarea><br />    <br/>
<textarea style="width:728px; font-size:14px; height:40px; font-weight:bold;   resize:none;" name="source" id="source" placeholder="Thread source" ></textarea><br />


<input type="submit" value="Post" name="submit" class="submit_button"/>
</form>

You could use the FileApi( http://www.w3.org/TR/FileAPI/ ) to get the image data and convert it to a dataURL, and send it as a post param(string), but that really depends on the browsers that you are supporting.您可以使用 FileApi( http://www.w3.org/TR/FileAPI/ ) 获取图像数据并将其转换为 dataURL,然后将其作为 post param(string) 发送,但这实际上取决于您支持的浏览器。 Alternatively you could use a hidden iframe as the forms target, so your page will not be redirected.或者,您可以使用隐藏的 iframe 作为表单目标,这样您的页面就不会被重定向。 See this link: http://www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html请参阅此链接: http : //www.9lessons.info/2011/08/ajax-image-upload-without-refreshing.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM