0

正如您可能从下面的代码中收集到的那样,我正在尝试使配置文件更新程序脚本正常工作。不过,我似乎遇到了问题。当脚本完成处理时,我收到“你好”警报,但似乎 jquery 没有正确地从文本框中获取值,因为我的 mysql 表没有返回任何插入的内容。有任何想法吗?下面是我的代码和截图:

在此处输入图像描述

前端代码:

<?php include("vh.php"); ?>
<script type="text/javascript">
    $(document).ready(function() {
    var birthdate = $("#birthdate").val();
    var sex = $("#sex :selected").val();
    var interestedIn = $("#interestedIn :selected").val();
    var relationshipStatus = $("#relationshipStatus :selected").val();
    var knownLanguages = $("#knownLanguages").val();
    var religiousViews = $("#religiousViews").val();
    var politicalViews = $("#politicalViews").val();
    var aboutMe = $("#aboutMe").val();
    var mobilePhone = $("#mobilePhone").val();
    var neighborhood = $("#neighborhood").val();
    var website = $("#website").val();
    var email = $("#email").val();

    $("#submitForm").live('click', function() {
        updateUserInfo();
    });

    function updateUserInfo() {
        jQuery.ajax({
            type: "POST",
            dataType: "JSON",
            url: "<?=base_url()?>index.php/regUserDash/updateUserInfo",
            data: { birthdate: birthdate,
                    sex: sex,
                    interestedIn: interestedIn,
                    relationshipStatus: relationshipStatus,
                    knownLanguages: knownLanguages,
                    religiousViews: religiousViews,
                    politicalViews: politicalViews,
                    aboutMe: aboutMe,
                    mobilePhone: mobilePhone,
                    neighborhood: neighborhood,
                    website: website,
                    email: email
            }, success: function(data) {
            if(data.userInfoUpdated == true) {
                alert("hello");
            }
          }
       });
    }
    });
</script>
<?php $selectedId = $_REQUEST['id'];
      $myuserid = $this->session->userdata('userid'); ?>
                        <table style="width: 34%">
                                <tr>
                                    <td style="width: 193px"><span class="font1">Birthdate</span></td>
                                    <td style="width: 275px">
                                    <input id="birthdate" class="textbox1" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px"><span class="font1">Sex</span></td>
                                    <td style="width: 275px">
                                    <select class="textbox1" id="sex" style="width: 207pt">
                                    <option selected="selected">Select your sex</option>
                                    <option>Male</option>
                                    <option>Female</option>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Interested In</td>
                                    <td style="width: 275px">
                                    <select class="textbox1" id="interestedIn" style="width: 207pt">
                                    <option selected="selected">Select your interest</option>
                                    <option>Men</option>
                                    <option>Women</option>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Relationship Status</td>
                                    <td style="width: 275px">
                                    <select class="textbox1" id="relationshipStatus" style="width: 207pt">
                                    <option selected="selected">Select your relationship status</option>
                                    <option>Single</option>
                                    <option>In a relationship</option>
                                    <option>Engaged</option>
                                    <option>Married</option>
                                    <option>It's complicated</option>
                                    <option>In an open relationship</option>
                                    <option>Widowed</option>
                                    <option>Seperated</option>
                                    <option>Divorsed</option>
                                    <option>In a civil union</option>
                                    <option>In a domestic partnership</option>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Known Languages</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="knownLanguages" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Religious Views</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="religiousViews" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Political Views</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="politicalViews" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">About Me<br />
                                    <br />
                                    <br />
                                    <br />
                                    </td>
                                    <td style="width: 275px">
                                    <textarea class="textarea2" id="aboutMe" style="width: 198pt; height: 93px"></textarea></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Mobile Phone</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="mobilePhone" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Neighborhood</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="neighborhood" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Website</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="website" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">Email</td>
                                    <td style="width: 275px">
                                    <input class="textbox1" id="email" style="width: 200pt" type="text" /></td>
                                </tr>
                                <tr>
                                    <td style="width: 193px" class="font1">&nbsp;</td>
                                    <td style="width: 275px; text-align: right">
                                    <input id="submitForm" type="button" value="Submit" style="width: 63px; height: 28px" class="button1"></td>
                                </tr>
                            </table>

后端代码:

public function updateUserInfo() {
        $userid = $this->session->userdata('userid');
        $birthdate = $this->input->post("birthdate");
        $sex = $this->input->post("sex");
        $interestedIn = $this->input->post("interestedIn");
        $relationshipStatus = $this->input->post("relationshipStatus");
        $languages = $this->input->post("languages");
        $religiousViews = $this->input->post("religiousViews");
        $politicalViews = $this->input->post("politicalViews");
        $aboutMe = $this->input->post("aboutMe");
        $mobilePhone = $this->input->post("mobilePhone");
        $neighborhood = $this->input->post("neighborhood");
        $websites = $this->input->post("websites");
        $email = $this->input->post("email");

        $this->db->query("INSERT INTO user_info (birthdate, sex, interestedIn, relationshipStatus, Languages, religiousViews, politicalViews, aboutMe, mobilePhone, neighborhood, websites, email, userid)
                          VALUES('{$birthdate}', '{$sex}', '{$interestedIn}', '{$relationshipStatus}', '{$languages}', '{$religiousViews}', '{$politicalViews}', '{$aboutMe}', '{$mobilePhone}', '{$neighborhood}', '{$websites}', '{$email}', '{$userid}')");

        echo json_encode(array('userInfoUpdated' => true));
    }
4

1 回答 1

3

当页面完成加载时,您会立即从表单中获取值,但此时它们是空的。当有人填写表格并单击提交时,函数会从变量生日、性别等中获取值,但它们是空的。

你必须移动变量赋值

var birthdate = $("#birthdate").val();
var sex = $("#sex :selected").val();
...

在 updateUserInfo() 函数中是这样的:

 data: { 
     birthdate: $("#birthdate").val(),
     sex: $("#sex").val(),
     ...
 }
于 2012-09-14T05:43:50.357 回答