dongzai3917 2015-07-27 07:13
浏览 63

如何将完成的json表单中的数据发送到php数据库

I want to run a code that allows automatic sending of the completed json form when internet connection is detected in the device. My ids for each of the question are dynamically generated. How do i send data on click of my submit button to the database while checking if there is internet connection if all my ids are dynamic?

<!DOCTYPE html> 
<html>
<head>
    <title>Form Page</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <link rel="stylesheet" type="text/css" href="css/panel2.css" />
    <link rel="stylesheet" type="text/css" href="css/jquery.mobile-1.4.5.min.css">
    <script src="js/jquery-2.1.4.min.js"></script>
    <script src="js/jquery.mobile-1.4.5.min.js"></script>

    <script src="js/rotation.js"></script> <!-- /rotation-->
    <script src="js/panel2.js"></script> <!-- /panel2-->
     <script type="text/javascript" src="cordova.js"></script>
    <script src="form.json"></script>
</head>

<body>
<script src="js/fullscreen.js"></script> <!-- /fullscreen-->
<div data-role="page" id="demo-page" data-url="demo-page">
    <div data-role="header" data-theme="b">
    <h1></h1>
    <a href="#left-panel" data-icon="carat-r" data-iconpos="notext" data-shadow="false" data-iconshadow="false" class="ui-nodisc-icon">Open left panel</a>
    </div><!-- /header -->
    <div role="main" class="ui-content jqm-content jqm-fullwidth" id="form-panel">
        <h3 class="textaligncenter">This is a Form</h3>       

        <div id="questions">
        </div>

        <script>
            var array = JSON.parse(arrayQuestions);
            console.log(array);
            for(var i=0;i<array.length;i++){
                var question = array[i];
                var questionDiv = document.createElement("div");
                var html = "<form>";
                html = question.question + " <br> ";
                var choices = question.choices;
                for(j=0;j<choices.length;j++){
                    var choicesOpt = choices[j];
                    if(question.type == "radio") {
                        html += "<fieldset data-role=\"controlgroup\">";
                        html += "<input type=\"radio\" name=\"" +i+ "\" id=\"" +i+choicesOpt+ "\" value=\"" +choicesOpt+ "\" /><label for=\"" +i+choicesOpt+ "\">" +choicesOpt+ "</label>";
                        html += "</fieldset>";
                    } else if(question.type == "checkbox"){
                        html += "<fieldset data-role=\"controlgroup\">";
                        html += "<input type=\"checkbox\" name=\"" +i+ "\" id=\"" +i+choicesOpt+ "\" value=\"" +choicesOpt+ "\" /><label for=\"" +i+choicesOpt+ "\">" +choicesOpt+ "</label>";
                        html += "</fieldset>";
                    } else if(question.type == "textbox"){
                        html += "<input type=\"text\" data-clear-btn=\"true\" name=\"" +i+ "\" id=\"" +i+choicesOpt+ "\" value=\"" +choicesOpt+ "\">";
                    } else {
                        html += "<textarea name=\"" +i+ "\" id=\"" +i+choicesOpt+ "\"></textarea>";
                    }
                    html += "";
                }
                html += "</form>";

//                html += "<input type=\"button\" onclick=\"myFunction()\" value=\"Submit form\"></form>";

                questionDiv.innerHTML = html + "<br>";
                document.getElementById('questions').appendChild(questionDiv);


            }
        </script>        
        <script>
            var buttonDiv = document.createElement("div");
                var buttonHtml = "<input type=\"button\" onclick=\"saveFunction()\" id=\"save\" value=\"Save Form\"></form>";
                buttonHtml += "<input type=\"button\" onclick=\"submitFunction()\" id=\"submit\" value=\"Submit Form\"></form>";
                buttonDiv.innerHTML = buttonHtml + "<br>";
                document.getElementById('questions').appendChild(buttonDiv);





            document.addEventListener("deviceready",onDeviceReady, false);
            document.addEventListener("online", OnlineEvent, false);
            document.addEventListener("offline", OnDeviceOffline, false);
            function onDeviceReady() {
            }
            function OnlineEvent(){   

                alert("You are online");
//          $('#submit').on('click', function () {
//            $.ajax({
//                url: "connection.php",
//                type: "POST",
//                data: {
//                    choice: $('#"\" value=\"" +choicesOpt+ "\" ').val(),
//                  
//                }
//                datatype: "json",
//                success: function (status) {
//                    if (status.success == false) {
//                        //alert a failure message
//                    } else {
//                        alert("Successfully submitted");
//                    }
//                }
//            });
//        });
    }

            function OnDeviceOffline(){
                alert("You are offline");
                }




           function saveFunction() {
               //Save storage here
//            window.location.href='status.html';
            if (OnlineEvent()== true){    
            $('#submit').on('click', function () {
            $.ajax({
                url: "connection.php",
                type: "POST",
                data: {
                    choice: $('#"\" value=\"" +choicesOpt+ "\" ').val(),

                }
                datatype: "json",
                success: function (status) {
                    if (status.success == false) {
                        //alert a failure message
                    } else {
                        alert("Successfully submitted");
                    }
                }
            });
        });
            }

            }
            function submitFunction() {

                 if (OnlineEvent()== true){    
            alert("Storage code here, send to server");

//               STORAGE FUNCTION CODE HERE alert("Your form is successfully submitted!");
                window.location.href='previewform.html';}
            }
              </script>


    </div><!-- /content -->

</div>

</body>
</html>






 <?php  

        $choice = json_decode($_POST['<script>choicesOpt</script>']);

        //And this is how, i suppose, we will add the values to my table 'sample'
        $sql = "INSERT INTO answers (choicesOpt) ";
        $sql .= "VALUES ('$choicesOpt')";
        if (!mysql_query($sql, $con)) {
            die('Error: ' . mysql_error());
        } else {
            echo "Answers stored";
        }
        mysql_close($con);

    ?>
  • 写回答

1条回答 默认 最新

  • double0201 2015-07-27 07:49
    关注

    Lets solve this step by step:

    1. First of for Automatically Checking of Internet Connection There is nice JS library for checking internet connectivity called Offline.js

    using

    document.addEventListener("online", OnlineEvent, false);
    document.addEventListener("offline", OnDeviceOffline, false);
    

    is highly unpredictable as this behaves differently in different browsers

    1. For getting values for automatically generated id their is JQuery utilities called jQuery.each() See details here
      Example :

    $('input[id^="txtAnswer"]').each(function(input){ var value = $('input[id^="txtAnswer"]').val(); var id = $('input[id^="txtAnswer"]').attr('id'); // you can now make a JSON containing answer or array with Key as post url parameter then make a ajax call after ending the loop and outside this loop });

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制