使用jQuery和php传递值

时间:2021-11-03 17:07:59

I want to pass the value of the file to catch php using jQuery. Is there a way to pass the value of the file to catch.php, so that var_dump($_FILES) will output somthing?

我想传递文件的值来使用jQuery捕获php。有没有办法将文件的值传递给catch.php,以便var_dump($ _ FILES)输出somthing?

------index.php------------

------ ------------的index.php

<p>Name: <input type="text" name="name" id="name" /></p>
<p>Picture: <input type="file" name="pic" id="pic" />)</p>
<p><a href="#" id="submit" >POST</a></p>
<div id="result"></div>
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$("#submit").click(function(){
    $.post(
       'catch.php',
       {name: $('#name').val(), pic: $('#pic').val()},
       function(result){
           $("#result").html(result);
       },
       "html"
    );
});
</script>

----------catch.php-----------------

---------- ----------------- catch.php

<?php
var_dump($_POST);

var_dump($_FILES);
?>

5 个解决方案

#1


1  

The javascript sandbox does not allow for uploading files with xhr. You can get around this limitation by submitting the form to a hidden iframe, but what you are trying to do simply won't work.

javascript沙箱不允许使用xhr上传文件。您可以通过将表单提交到隐藏的iframe来解决此限制,但您尝试执行的操作将无法正常工作。

#2


0  

If you want to do a file upload - or just need the file name, cou can find some inforamtion about that at this link.

如果你想进行文件上传 - 或者只是需要文件名,可以在这个链接上找到一些关于它的信息。

#3


0  

As an alternative, the YUI framework has an uploader component to solve this problem -- it uses the hidden iframe approach listed above but handles it for you transparently.

作为替代方案,YUI框架有一个上传器组件来解决这个问题 - 它使用上面列出的隐藏的iframe方法,但是透明地为你处理它。

#4


0  

This is untested, but I imagine it's something similar to this. Please, submit edits.

这是未经测试的,但我想它与此类似。请提交修改。

javascript:

JavaScript的:

$("button").click(function(){
  $.post("photobridge.php",
  {
    filepath="/images/your_image.png";
    name="Image Name"
  },
  function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

php:

PHP:

<?php
$filepath=Request.Form("filepath")
$name=Request.Form("name")
HTTPResponse::send("Image location".$filepath.";")
HTTPResponse::send("Image name".$name.".")

#5


-1  

You don't need jQuery for this - you just want a submit button on your form.

你不需要jQuery - 你只需要在表单上提交一个提交按钮。

So:

所以:

<form action='catch.php' method='post' />

#1


1  

The javascript sandbox does not allow for uploading files with xhr. You can get around this limitation by submitting the form to a hidden iframe, but what you are trying to do simply won't work.

javascript沙箱不允许使用xhr上传文件。您可以通过将表单提交到隐藏的iframe来解决此限制,但您尝试执行的操作将无法正常工作。

#2


0  

If you want to do a file upload - or just need the file name, cou can find some inforamtion about that at this link.

如果你想进行文件上传 - 或者只是需要文件名,可以在这个链接上找到一些关于它的信息。

#3


0  

As an alternative, the YUI framework has an uploader component to solve this problem -- it uses the hidden iframe approach listed above but handles it for you transparently.

作为替代方案,YUI框架有一个上传器组件来解决这个问题 - 它使用上面列出的隐藏的iframe方法,但是透明地为你处理它。

#4


0  

This is untested, but I imagine it's something similar to this. Please, submit edits.

这是未经测试的,但我想它与此类似。请提交修改。

javascript:

JavaScript的:

$("button").click(function(){
  $.post("photobridge.php",
  {
    filepath="/images/your_image.png";
    name="Image Name"
  },
  function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
  });
});

php:

PHP:

<?php
$filepath=Request.Form("filepath")
$name=Request.Form("name")
HTTPResponse::send("Image location".$filepath.";")
HTTPResponse::send("Image name".$name.".")

#5


-1  

You don't need jQuery for this - you just want a submit button on your form.

你不需要jQuery - 你只需要在表单上提交一个提交按钮。

So:

所以:

<form action='catch.php' method='post' />