douya1061 2013-09-01 13:50
浏览 54

使用jquery和ajax在同一页面中加载php表单

I want a function that loads the values from a PHP form and show the result in a div in the same file ;

I have a form in PHP file with the code bellow :

<?php

 echo "<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'></script>";
echo"<script type='text/javascript' src='script.js'></script>";


echo"<form method='post' action=''  >";
echo " <select name='mat'><option value=''>Choose: </option>";
echo"<option >hello</option>";
echo"</select>";
echo"<input type = 'submit'  value='next' onclick='submit();' />";
echo"</form>";

echo"<div id ='result'> </div>";
?>

and the function in script.js is :

function submit() {
    $(function () {
        var mat = "mat";
        var dataString = 'mat=' + mat;
        $.ajax({
            type: "POST",
            url: "check.php",
            data: dataString,
            success: function (res) {
                goToByScroll("result");
                $('#result').html("<br><br><br><br><br><br><br><br><br><br>").hide().fadeIn(2500, function () {
                    $('#result').html(res + "<br /><br /> Finished");
                });
            }
        });
        return false;
    });
}

and the check.php that load the values is :

<?php 
function safe($value)
{
    htmlentities($value, ENT_QUOTES, 'utf-8');
    return $value;
}
if (isset($_POST['mat'])) {
    $mat = safe($_POST['mat']);
    echo ($mat);
}
elseif (!isset($_POST['mat'])) {
    header('Location: error?mat=notfound');
} ?>

But when I click next in the fist PHP file, it doesn't show anything, can you find the error?

  • 写回答

1条回答 默认 最新

  • duanlujiaji10335 2013-09-01 13:52
    关注

    You are not executing the $(function(){ .. inside the submit function. You have to remove that line so you will get this:

    function submit() {
        var mat = "mat";
        var dataString = 'mat=' + mat;
        $.ajax({
            type: "POST",
            url: "check.php",
            data: dataString,
            success: function(res) {
                goToByScroll("result");
                $('#result').html("<br><br><br><br><br><br><br><br><br><br>").hide().fadeIn(2500, function() {
                    $('#result').html(res + "<br /><br /> Finished");
                });
            }
        });
        return false;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题