Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.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 无法通过formdata检索数据_Php_Jquery_Ajax_Form Data - Fatal编程技术网

Php 无法通过formdata检索数据

Php 无法通过formdata检索数据,php,jquery,ajax,form-data,Php,Jquery,Ajax,Form Data,我正在尝试使用ajax保存图像,并通过Formdata()传递数据 但在php文件中,我无法检索数据或图像名称,请帮助我 这是我的密码 <form name='signup' id='signup'> <div class="row"> <!--<form id="uploadimage" action="" method="post" enctype="multipart/form-data">--> &l

我正在尝试使用ajax保存图像,并通过Formdata()传递数据 但在php文件中,我无法检索数据或图像名称,请帮助我 这是我的密码

<form name='signup' id='signup'>
    <div class="row">
        <!--<form id="uploadimage" action="" method="post" enctype="multipart/form-data">-->
        <div id="selectImage">
            <label>Select Image</label>
            <div id="image_preview">
                <img id="previewing" src="uploaded_files/259700.png" height="150" width="150" />
            </div>
            <input type="file" name="file" id="file" required />
            <!--<input type="submit" value="Upload" class="submit" />-->
        </div>
        <!--</form>-->
    </div>
    <div class='row'>
        <p>
            <label for='username'>First name</label>
            <input type='text' name='firstname' id='firstname' value='' placeholder='Enter First name' />
        </p>
    </div>
    <div class='row'>
        <p>
            <label for='lastname'>Last name</label>
            <input type='text' name='lastname' id='lastname' value='' placeholder='Enter Last name' />
        </p>
    </div>
    <div class='row'>
        <p>
            <label for='email'>Email</label>
            <input type='text' name='email' id='email' value='' placeholder='Enter Email' />
        </p>
    </div>
    <div class='row'>
        <p>
            <label for='phno'>Phno.</label>
            <input type='text' name='phno' id='phno' maxlength="10" value='' placeholder='Enter ph no' />
        </p>

    </div>
    <!--<input type="hidden" name="actionfunction" value="saveData" />-->
    <input type="hidden" name="actionfunction" value="saveData" />
    <div class='row'>
        <input type='button' id='formsubmit' class='submit' value='Submit' />
        <!--<input type='submit' id='formsubmit' class='submit' value='Submit' />-->
    </div>
</form>
下面是我的“DbManipute.php”代码:

我没有得到任何错误,数据也没有插入。 当我删除图像上载并使用serialize方法时,数据成功保存,但serialize方法中的图像文件名无法检索
请帮我解决这个问题

您需要单独附加
type=“file”

$("#signup").on('submit', (function() {
  var fname = $("#firstname");
  var lname = $("#lastname");
  var email = $("#email");
  var phno = $("#phno");
  if (validateform(fname, lname, email, phno)) {
    var formdata = new FormData(this);
    formdata.append( 'file', $("input[name='file']")[0].files[0] );
    $.ajax({
      url: "DbManipute.php",
      type: "POST",
      data: formdata,
      processdata: false,
      cache: false,
      contentType: false,
      success: function(response) {
        //alert(response);
        if (response == 'added') {
          $("#show_user").trigger("click");
          getusers();
          $("#msg").html("user added");
        }
      },
    });
  }
});

客户端错误不会像php一样显示在页面中。 您的javascript代码中有一些错误。您错过了js代码末尾的右括号:
})

如果您使用的是chrome或firefox浏览器,请按
F12
按钮,此时将显示控制台,并显示您的js错误

提示:您可以通过打印代码中的任何变量来调试项目。例如,尝试使用
console.log('test')
在js代码中检查ajax命令是否正常工作
console.log()
将在浏览器的控制台框中打印

当我在你的代码中这样做时,我知道你的代码根本没有运行。因为您已将代码设置为在submit上运行,并且没有任何submit按钮。然后您需要更改按钮的
type=“submit”

之后,您需要使用浏览器提交表单,并告诉浏览器您想要运行js代码,从而阻止表单。为此,您需要防止表单提交事件的默认操作,如下所示:

$("#signup").on('submit', (function(evt) {
    evt.preventDefault();
另一个提示是,php代码位于从未调用过的函数中。您需要在php文件中调用php函数,否则应该将代码放在函数之外

尝试像这样更改代码。这将使您的代码正常工作。要检查ajax请求是否正常工作,请尝试在代码和js代码中回显响应


<>强> >编辑1 < /强>要考虑的是,对于想要使用Ajax上传文件的情况,不能设置数据“<代码> FrasDATA(this)”。出于ajax上传目的,您应该创建FormData对象(
newFormData()
),并单独附加数据。(正如Rejith R Krishnan所说)。

我已经按照您的建议添加了行。但仍然无法检索数据。@suh try
$。trim(response)==“添加”
。感谢您的建议,但它仍然不起作用。
$("#signup").on('submit', (function() {
  var fname = $("#firstname");
  var lname = $("#lastname");
  var email = $("#email");
  var phno = $("#phno");
  if (validateform(fname, lname, email, phno)) {
    var formdata = new FormData(this);
    formdata.append( 'file', $("input[name='file']")[0].files[0] );
    $.ajax({
      url: "DbManipute.php",
      type: "POST",
      data: formdata,
      processdata: false,
      cache: false,
      contentType: false,
      success: function(response) {
        //alert(response);
        if (response == 'added') {
          $("#show_user").trigger("click");
          getusers();
          $("#msg").html("user added");
        }
      },
    });
  }
});
$("#signup").on('submit', (function(evt) {
    evt.preventDefault();