如何在不刷新頁面的情況下在同一頁面上提交表單。我知道ajax是可能的,但我並不完美,它使用我的本地編程語言PHP。在同一頁面上發帖而不刷新
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);
}
?>
你不能沒有Ajax做。 –
@PatsyIssa他沒有說他正在尋找另一種解決方案,他只是指出,他的主要技能是PHP。 – DannyThunder
在這裏你可以找到一個準備使用的jquery插件的一些例子也http://malsup.com/jquery/form/ – Petra