如何在不刷新页面的情况下在同一页面上提交表单。我知道使用 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);
}
?>