Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php在onsubmit内部的页面加载时激活_Php_Onsubmit - Fatal编程技术网

php在onsubmit内部的页面加载时激活

php在onsubmit内部的页面加载时激活,php,onsubmit,Php,Onsubmit,我有一个php函数,可以在提交表单时在目录中创建一个文件。 代码是: <form id="myForm" onSubmit="<?php error_reporting(E_ALL); $directory = __DIR__ . "/images/"; $filecount = 0; $files = glob($directory . "*"); if ($files){ $file

我有一个php函数,可以在提交表单时在目录中创建一个文件。 代码是:

<form id="myForm" onSubmit="<?php
    error_reporting(E_ALL);
    $directory = __DIR__ . "/images/"; $filecount = 0; $files = glob($directory . "*");  if ($files){ 
    $filecount = count($files); }
    $filecount = $filecount + 1;
    $pagename = 'image_'.$filecount;

    $newFileName = './images/'.$pagename;
    $newFileContent = 'Page Content';
    if (file_put_contents($newFileName, $newFileContent) !== false) {
    echo "File created (" . basename($newFileName) . ")";
    } else {
    echo "Cannot create file (" . basename($newFileName) . ")";
    }
?>">
       <input placeholder="Username 0/25" name="user" id="user" maxlength="25" required>
       <input placeholder="Password 0/45" name="password" id="password" maxlength="45" required>
       <input placeholder="Image Name" name="name" id="name">
       <input type="file" placeholder="Image" accept="image/*" name="img" required>
       <button type="submit">Upload</button>
       </form>

问题是它会在页面加载时自动激活

为了防止在按submit时自动加载页面,您可以使用此技巧
“return false;”\“…”
,但它不会创建图像文件:/

if(文件内容($newFileName,$newFileContent)!==false){
echo“return false;\”创建的文件(“.basename($newFileName)。”);
}否则{
echo“return false;\”无法创建文件(“.basename($newFileName)。”;
}
我试着模仿你的项目,但是我用了txt文件而不是图片。 我曾经

index.html

创建一个文件
script.js
document.getElementById('myForm')。onsubmit=function(e){
e、 预防默认值();
var xhr=new XMLHttpRequest();
open('GET','creating.php');
xhr.onreadystatechange=函数(){
console.log(xhr.readyState)
if(xhr.readyState==XMLHttpRequest.DONE)
if(xhr.response.startsWith('Woow'))
警报(“文件创建成功!”);
其他的
警惕(“出了什么事!”);
}
xhr.send();
}
创建.php


你的代码完全错误。你不能在Javascript事件处理程序中运行PHP代码。Javascript在浏览器中运行,PHP在服务器上运行。实现这一点的方法是使用AJAX向服务器发出请求,并在服务器上运行PHP代码。你有@TangentialyVertical的例子吗?对不起,我对PHP非常陌生,没有太熟悉javascript了