【问题标题】:Insert data to cpanel database将数据插入cpanel数据库
【发布时间】:2016-07-05 06:35:57
【问题描述】:

所以我正在开展这个项目,用户可以在其中注册新闻通讯,并将他们的名字添加到在线数据库中。我正在使用在线数据库 (CPanel - PhPMyAdmin) 我一直纠结于应该使用哪种类型的代码将表单中的数据添加到数据库中。

这是我到目前为止所拥有的,我不太确定如何从这里开始。任何帮助表示赞赏。

另外:我正在使用 Bootstrap3 和 jquery/ajax

<div id="newsletter" class="container-fluid text-center">
            <h3>NEWSLETTER</h3>

            <form id="signup-form" action="" method="">
              <div class="input-prepend" ><span class="add-on"><i class="glyphicon glyphicon-user"></i></span>
                <label for="name">Your Full Name</label>
                <input type="text" id="name" name="name" placeholder="your full name" size="40" style="color:black;" class="validate" required>
              </div>

              <div class="input-prepend"><span class="add-on"><i class="glyphicon glyphicon-envelope"></i></span>
                <label for="email">Your Email</label>
                <input type="email" id="email" name="email" placeholder="your@email.com" size="40" style="color:black;"class="validate" required>
              </div>

              <button id="sub" type="submit" class="btn btn-large"> Sign Up!</button>
            </form>
            <br/>

</div>

【问题讨论】:

    标签: php jquery ajax twitter-bootstrap


    【解决方案1】:

    您可以使用 Ajax

    示例 -

    $('#button').click(function() {//give id of submit button 
        var val1 = $('#text1').val();//get value you need to post
        var val2 = $('#text2').val();//get value you need to post
        $.ajax({
            type: 'POST',
            url: 'process.php',
            data: { text1: val1, text2: val2 },
            success: function(response) {
                $('#result').html(response);//responce you recevied 
            }
        }); });
    

    使用 Post 方法获取两个值。执行您的操作并在结果中发送 true false。

    ----------------用新的例子深入解释----

    HTML 文件:ajaxsubmit.html

    <!DOCTYPE html>
    <html>
    <head>
    <title>Submit Form Using AJAX and jQuery</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <link href="css/refreshform.css" rel="stylesheet">
    <script src="script.js"></script>
    </head>
    <body>
    <div id="mainform">
    <h2>Submit Form Using AJAX and jQuery</h2> <!-- Required div Starts Here -->
    <div id="form">
    <h3>Fill Your Information !</h3>
    <div>
    <label>Name :</label>
    <input id="name" type="text">
    <label>Email :</label>
    <input id="email" type="text">
    <label>Password :</label>
    <input id="password" type="password">
    <label>Contact No :</label>
    <input id="contact" type="text">
    <input id="submit" type="button" value="Submit">
    </div>
    </div>
    </div>
    </body>
    </html>
    

    PHP 文件:ajaxsubmit.php

    <?php
    $connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server..
    $db = mysql_select_db("mydba", $connection); // Selecting Database
    //Fetching Values from URL
    $name2=$_POST['name1'];
    $email2=$_POST['email1'];
    $password2=$_POST['password1'];
    $contact2=$_POST['contact1'];
    //Insert query
    $query = mysql_query("insert into form_element(name, email, password, contact) values ('$name2', '$email2', '$password2','$contact2')");
    echo "Form Submitted Succesfully";
    mysql_close($connection); // Connection Closed
    ?>
    

    jQuery 文件:script.js

    $(document).ready(function(){
    $("#submit").click(function(){
    var name = $("#name").val();
    var email = $("#email").val();
    var password = $("#password").val();
    var contact = $("#contact").val();
    // Returns successful data submission message when the entered information is stored in database.
    var dataString = 'name1='+ name + '&email1='+ email + '&password1='+ password + '&contact1='+ contact;
    if(name==''||email==''||password==''||contact=='')
    {
    alert("Please Fill All Fields");
    }
    else
    {
    // AJAX Code To Submit Form.
    $.ajax({
    type: "POST",
    url: "ajaxsubmit.php",
    data: dataString,
    cache: false,
    success: function(result){
    alert(result);
    }
    });
    }
    return false;
    });
    });
    

    MY-SQL 代码段:

    CREATE DATABASE mydba;
    CREATE TABLE form_element(
    id int(10) NOT NULL AUTO_INCREMENT,
    name varchar(255) NOT NULL,
    email varchar(255) NOT NULL,
    password varchar(255) NOT NULL,
    contact varchar(255) NOT NULL,
    PRIMARY KEY (id)
    )
    

    【讨论】:

    • process.php 是干什么用的?
    • 我没有确切地告诉你什么 process.php ?我正在根据您的问题更新我的答案以指导其中的内容
    • 哦,我真的没有任何 php 文件 atm,所以我对它的用途感到困惑
    • 谢谢!!我知道了! :D
    • 对不起一个问题!当我单击提交时,它显示 ajaxsubmit.php 代码作为警报而不是结果?我哪里错了?
    猜你喜欢
    • 2014-05-29
    • 2016-02-24
    • 2020-07-05
    • 2023-04-10
    • 2021-10-21
    • 2014-08-08
    • 2014-05-04
    相关资源
    最近更新 更多