php – 如何在jQuery Mobile页面中提交表单?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何在jQuery Mobile页面中提交表单?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 HTML主页,它使用jquery mobile的UI.在HTML页面的某个地方,我有一个类似于投票表单的表单:
<form name = "Vote" action = "LOGicDB.PHP" method = "post">
                    <center>
                        <h3>Watch our videos above and share your opinion which manufacturer PRoduces the best sport sedans</h3>                
                        <fieldset style = "width:60%;">
                            <legend>Choose a car:</legend>
                            <input tyPE = "radio" name = "rc1" id = "radio-choice-1" value = "Audi"/>
                            <input type = "radio" name = "rc1" id = "radio-choice-2" value = "BMW"/>
                            <input type = "radio" name = "rc1" id = "radio-choice-3" value = "Mercedes"/>           
                        </fieldset>                 
                        <input value = "SubMIT" type = "submit" />
                    </center>
</form>

表单的action参数调用我的PHP文件,该文件具有以下逻辑:

<?PHP
$connection = MysqL_connect("localhost","root","myPassword");
if(!$connection){ die('Could not connect: ' . MysqL_error()); }

MysqL_select_db("mydatabase",$connection);

if ($_POST['rc1'] == 'Audi')
{   
    MysqL_query("
    UPDATE carVote
    SET Votes = Votes + 1
    WHERE brands = 'Audi'
    ",$connection);

    echo "success Audi within if";
}

if ($_POST['rc1'] == 'BMW')
{
    MysqL_query("
    UPDATE carVote
    SET Votes = Votes + 1
    WHERE Brands = 'BMW'
    ",$connection);

    echo "success BMW within if";
}

if ($_POST['rc1'] == 'Mercedes')
{
    MysqL_query("
    UPDATE carVote
    SET Votes = Votes + 1
    WHERE Brands = 'Mercedes'
    ",$connection);

    echo "success Mercedes within if";
}
MysqL_close($connection);
?>

我在我的WAMP localhost服务器上创建了一个数据库,这个PHP代码正在写入数据库,具体取决于从表单中选择和提交的无线选项.从未达到过PHP中的这些回声.

问题是,当我点击提交时,我会在@L_126_42@看到一个白色的屏幕,显示“未定义”,之后没有任何内容写入数据库.我发现,如果我从html主页中删除调用顶部jQuery mobile的行
一切正常,数据写入数据库(到达PHP中的回声).
似乎jQuery mobile与我的PHP代码不兼容.我该怎么解决这个问题?
先感谢您!

更新(我使它工作):
以下是我在表单中所做的更改:

<form id = "ContactForm" onsubmit="return submitForm();">

带有数据库PHP文件保持不变.除此之外,我还添加一个Ajax代码

function submitForm()
    {
        $.ajax({type:'POST',url: 'logicDB.PHP',data:$('#ContactForm').serialize(),success: function(response)
        {
            $('#ContactForm').find('.form_result').html(response);
        }});
        return false;
    }

总结一下:jQuery Mobile页面(div中带有data-role =“page”的页面)只能通过AJAX提交.它不会起作用.
我会将主题改为我的问题,以便更多人可以达到它.

我复制了你的代码并插入了jquery mobile,在进行了一些小的修改之后我发布了数据.我通过echo命令运行它,而不是DB插入.

看看以下内容

调用Jquery Mobile:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>;my Page</title> 
    <Meta name="viewport" content="width=device-width,initial-scale=1"> 
    <link rel="stylesheet" href="http://code.jquery.COM/mobile/1.1.0/jquery.mobile-1.1.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script>
</head>

其余的index.PHP页面

<div data-role="page">
<center>
    <h3>Watch our videos above and share your opinion which manufacturer produces the best sport sedans</h3>                
    <form name="Vote" action="logicDB.PHP" method="post" data-ajax="false">
        <fieldset style = "width:60%;" data-role="controlgroup">
            <legend>Choose a car:</legend>
            <input type = "radio" name="rc1" id = "radio-choice-1" value = "Audi"/>
            <label for="radio-choice-1">Audi</label>

            <input type = "radio" name="rc1" id = "radio-choice-2" value = "BMW"/>
            <label for="radio-choice-2">BMW</label>

            <input type = "radio" name="rc1" id = "radio-choice-3" value = "Mercedes"/>
            <label for="radio-choice-3">Mercedes</label>                            
        </fieldset>                 
        <input value = "SUBMIT" type = "submit" />
    </form>
</center>

最后是logicDB.PHP

if ($_POST['rc1'] == 'Audi') {
 echo "Audi <br>";
}
if ($_POST['rc1'] == 'BMW') {
 echo "BMW <br>";
}
if ($_POST['rc1'] == 'Mercedes') {
 echo "Mercedes <br>";
}

主要的变化是

<fieldset style = "width:60%;" data-role="controlgroup">

和之前提到的data-ajax =“false”.

看看它是否适合你.

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何在jQuery Mobile页面中提交表单?全部内容,希望文章能够帮你解决php – 如何在jQuery Mobile页面中提交表单?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。