如何使用 Jquery.form.js 从 php 调用获取数据?

发布于 2024-12-08 11:36:19 字数 2852 浏览 0 评论 0原文

我正在使用 jquery.form.js 中的 AjaxForm() 调用来尝试运行一个 php 脚本,该脚本将数据放入数据库然后将其取出。但是如何使用 jquery.form.js 中的 ajaxForm 调用来进行该调用呢?

*更新:添加了一些代码 *

    $('#profilepicbutton').live('change', function(){
    $("#preview").html('');
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
        $("#registerpt3").ajaxForm({target: '#preview'}).submit();
        /*$.post(
        "register3.php",{
            target:'#preview'},
            function(data){*/
                $("#preview").html('');
                $("#preview").append("<img src="+data+"></img>");
 });

我的 PHP 代码...

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
  $name = $_FILES['profilepicinput']['name'];
  $size = $_FILES['profilepicinput']['size'];
  if(strlen($name))
  {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
        if($size<(1024*1024)) // Image size max 1 MB
        {
            $actual_image_name = time().$session_id.".".$ext;
            $tmp = $_FILES['profilepicinput']['tmp_name'];
            $fp      = fopen($tmp, 'r');
            $data = fread($fp, filesize($tmp));
            $data = addslashes($data);
            fclose($fp);
            if(mysql_query("insert into Personal_Photos (Email, Pics) values('$email', '$data')"))
            {
                $query="select Pics, MAX(ID) from Personal_Photos where Email='$email'";
                $result=mysql_query($query) or die("Error: ".mysql_error());
                $row=mysql_fetch_array($result);
                header("Content-type: image/jpg");
                print($row['Pics']);
            }
            else
            {
                die('Invalid query: ' . mysql_error());
                echo "failed";
            }
        }
        else
            echo "Image file size max 1 MB. Image Size:"+$size;
    }
    else
        echo "Invalid file format..";
}
else
    echo "Please select image..! Bull shit".$email;
exit;

}

所以你是说数据应该自动神奇地返回?

当我调用 ajaxForm 时我应该调用一个函数吗?

更新:请求示例

你好。我不想成为那个不断要求示例代码的人,但是是否有人有一个进行 ajaxForm() 调用并成功取回数据并将其发布到该页面的示例?我不知道该怎么做...我刚刚尝试的是

$('#profilepicbutton').live('change', function(){
    $("#preview").html('');
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
        $("#registerpt3").ajaxForm({
                target: '#preview',
                success: function(data)
                {                                   
                    $("#preview").html('');
                    $("#preview").append("<img src="+data+"></img>");
                }
            }).submit();
 });

当我使用 firebug 来找出数据中的内容时,它告诉我它是未定义的。真的很困惑。请帮忙。

I am using the AjaxForm() call from jquery.form.js to attempt to run a php script that will put data into the data base and then pull it out. But how would you make that call using the ajaxForm call from jquery.form.js?

*UPDATE: ADDED SOME CODE *

    $('#profilepicbutton').live('change', function(){
    $("#preview").html('');
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
        $("#registerpt3").ajaxForm({target: '#preview'}).submit();
        /*$.post(
        "register3.php",{
            target:'#preview'},
            function(data){*/
                $("#preview").html('');
                $("#preview").append("<img src="+data+"></img>");
 });

My PHP code...

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
    {
  $name = $_FILES['profilepicinput']['name'];
  $size = $_FILES['profilepicinput']['size'];
  if(strlen($name))
  {
    list($txt, $ext) = explode(".", $name);
    if(in_array($ext,$valid_formats))
    {
        if($size<(1024*1024)) // Image size max 1 MB
        {
            $actual_image_name = time().$session_id.".".$ext;
            $tmp = $_FILES['profilepicinput']['tmp_name'];
            $fp      = fopen($tmp, 'r');
            $data = fread($fp, filesize($tmp));
            $data = addslashes($data);
            fclose($fp);
            if(mysql_query("insert into Personal_Photos (Email, Pics) values('$email', '$data')"))
            {
                $query="select Pics, MAX(ID) from Personal_Photos where Email='$email'";
                $result=mysql_query($query) or die("Error: ".mysql_error());
                $row=mysql_fetch_array($result);
                header("Content-type: image/jpg");
                print($row['Pics']);
            }
            else
            {
                die('Invalid query: ' . mysql_error());
                echo "failed";
            }
        }
        else
            echo "Image file size max 1 MB. Image Size:"+$size;
    }
    else
        echo "Invalid file format..";
}
else
    echo "Please select image..! Bull shit".$email;
exit;

}

So you are saying that the data should come right back auto-magically?

When I call ajaxForm should I call a function?

UPDATE: Request for an example

hello there. I don't want to be that dude that is constantly asking for example code, but does any body have an example of making an ajaxForm() call and successfully getting data back and posting it to there page? I can't figure out what to do... What I just tried is

$('#profilepicbutton').live('change', function(){
    $("#preview").html('');
    $("#preview").html('<img src="loader.gif" alt="Uploading...."/>');
        $("#registerpt3").ajaxForm({
                target: '#preview',
                success: function(data)
                {                                   
                    $("#preview").html('');
                    $("#preview").append("<img src="+data+"></img>");
                }
            }).submit();
 });

When I use firebug to figure out what is in data, it is telling me that it is undefined. Really confused. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方圜几里 2024-12-15 11:36:19

您只需执行: $('#myFormId').ajaxForm(); ,它就会自动神奇地工作:)
您可以将任何标准 $.ajax 选项传递给 ajaxForm(完成和成功回调等)。

您可以在此处查看完整的文档: http://jquery.malsup.com/form/#getting -开始

You simply do: $('#myFormId').ajaxForm(); and it should auto-magically work :)
You can pass any of the standard $.ajax options to ajaxForm (complete and success callbacks, etc).

You can see the complete docs here: http://jquery.malsup.com/form/#getting-started

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文