如何在不刷新页面的情况下在同一页上提交表单。我知道可以使用ajax实现,但我的本地编程语言是PHP,对ajax并不熟悉。
HTML
<input type="submit" class="topopup1" value="Preview" onclick="capture();" >
<form method="POST" enctype="multipart/form-data" id="myForm">
<input type="hidden" name="img_val" id="img_val" value="" />
</form>
Javascript
function capture() {
$('.showcase').html2canvas({
onrendered: function (canvas) {
//Set hidden field's value to image data (base-64 string)
$('#img_val').val(canvas.toDataURL("image/png"));
//Submit the form manually
document.getElementById("myForm").submit();}
});
}
PHP
<?php
//Show the image
if(isset($_POST['img_val'])){
echo '<img src="'.$_POST['img_val'].'" />';
//Get the base-64 string from data
$filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1);
//Decode the string
$unencodedData=base64_decode($filteredData);
//Save the image
file_put_contents('img.png', $unencodedData);
}
?>