简体   繁体   English

jQuery Mobile使用不含AJAX和JavaScript的PHP提交

[英]jQuery Mobile submit with PHP without AJAX and JavaScript

Sorry, I consider myself as a real newbie around jQuery Mobile. 抱歉,我认为自己是jQuery Mobile的真正新手。 I'm not good at all regarding JavaScript. 关于JavaScript我一点也不擅长。 Here's the thing. 就是这个 I want to build a jQuery Mobile site without using AJAX. 我想建立一个不使用AJAX的jQuery Mobile网站。 Just want the nice design from jQuery Mobile and then use PHP to submit forms etc. 只需要jQuery Mobile的精美设计,然后使用PHP提交表单等。

I tried to build a simple page that submit first and last name to a MySQL database. 我试图构建一个简单的页面,将名字和姓氏提交到MySQL数据库。 It will submit, tell the user that it's submitted and then the user can press [Page 2] to see all the results. 它将提交,告诉用户已提交,然后用户可以按[Page 2]查看所有结果。 Now I use if(isset()) to display the message and else to display the form. 现在,我使用if(isset())显示消息,否则显示表单。 So, the user who enter the site will get the form, when press [Submit] he/she will get the message that first and last name was submitted. 因此,进入站点的用户将获得表格,当按[Submit]时,他/她将收到有关姓氏和名字已提交的消息。 Then press the button [Page 2] to see all the first and last names. 然后按[Page 2]按钮查看所有名字和姓氏。

PHP (index.php) PHP(index.php)

if(isset($_POST['send'])) {

    $insert = $db->prepare("INSERT INTO name (fname, lname) VALUES(:fname, :lname)");
    $insert_array = array(
        ":fname" => $_POST['fname'],
        ":lname" => $_POST['lname']
    );

    $insert->execute($insert_array);

    $db = NULL;

    echo $_POST['fname'] . ' ' . $_POST['lname'] . ' was added!<br><br>';
}

else {
    echo '
    <form method="post" data-ajax="false">

        First name:
        <input type="text" name="fname"><br>

        Last name:
        <input type="text" name="lname"><br>

        <input type="submit" name="send" value="Add">

    </form><br>';
}

<a href="page2.php" data-role="button">Page 2</a>

PHP (page2.php): PHP(page2.php):

$query = $db->query("SELECT * FROM name");

while($row = $query->fetch(PDO::FETCH_ASSOC)) {
    echo $row['fname'] . ' ' . $row['lname'] . '<br>';
}

$db = NULL;

echo '<a href="index.php" data-role="button">Index</a>';

Let's say I enter "Test" as first and last name. 假设我输入“ Test”作为名字和姓氏。 It will echo out "Test Test was added!". 它将回显“已添加测试测试!”。 If I now press [Page 2] I will see that "Test Test" was added. 如果现在按[Page 2],我将看到已添加“测试测试”。 BUT when I then press [Index] to go back I want it to display the form again, but the message "Test Test was added!" 但是,当我然后按[索引]返回时,我希望它再次显示该表单,但是消息“已添加测试”! is displayed again instead of the form, why? 再次显示而不是表格,为什么? I have to update the page to get the form. 我必须更新页面以获得表格。 Now, if I enable data-ajax it's working with submitting and back-button. 现在,如果启用了数据ajax,则可以使用提交和后退按钮。 BUT then I have to press update at page2.php when I get there to see all the first and last names. 但是 ,当我到达那里看到所有的名字和姓氏时,我必须按page2.php上的update。 Do I make myself understood what's the problem? 我是否使自己了解问题所在?

Sorry, really new at jQuery Mobile and I can't find the answer at Google. 抱歉,这是jQuery Mobile的新功能,我在G​​oogle找不到答案。 Everyone is using JavaScript to submit data. 每个人都在使用JavaScript提交数据。 Is it possible this way or do I have to learn JavaScript to submit forms? 是否可以通过这种方式进行?还是我必须学习JavaScript才能提交表单? Read somewhere that using buttons instead of submit-buttons affect it. 在某处阅读使用按钮而不是提交按钮会影响它的地方。

Thanks in advance! 提前致谢! :) :)

I think you are looking to modify the DOM after the request? 我认为您在请求后是否希望修改DOM? So post the form, add the user then display the results without having to click the button. 因此,发布表单,添加用户,然后显示结果,而无需单击按钮。

So on your ajax call use the done function to hide the form and show the results. 因此,在您的ajax调用中,请使用done函数隐藏表单并显示结果。

Take a look below and let me know if it helps. 请看下面,让我知道是否有帮助。

EDIT: Added the .on click for the button. 编辑:添加了.on单击按钮。 You may also want to look at adding a keypress checker to the inputs or an onsubmit on the form. 您可能还需要查看将按键检查器添加到输入或表单上的onsubmit。

<div id="content">
<?php
if(isset($_POST['send'])) {

    $insert = $db->prepare("INSERT INTO name (fname, lname) VALUES(:fname, :lname)");
    $insert_array = array(
        ":fname" => $_POST['fname'],
        ":lname" => $_POST['lname']
    );

    $insert->execute($insert_array);

    $db = NULL;

    echo $_POST['fname'] . ' ' . $_POST['lname'] . ' was added!<br><br>';
}

else {
    echo '
    <form method="post" data-ajax="false" id="contentForm">

        First name:
        <input type="text" name="fname"><br>

        Last name:
        <input type="text" name="lname"><br>

        <input type="submit" name="send" value="Add" id="sendButton">

    </form><br>';
}
?>
</div>

<script type='text/javascript'>
<!-- https://api.jquery.com/jQuery.ajax/ -->
$('#sendButton').on("click", function(){ 
    $.ajax({
      type: "POST",
      url: "page2.php",
      data: $('#contentForm').serialize()
    })
      .done(function( msg ) {
        $('#content').html( msg );
    });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM