0

大家好,我正在创建“帐单地址”“送货地址”表格。当有人填写帐单地址详细信息并且交货相同时,我想保存一个人来勾选框以填充详细信息。

我试图用 javascript 和 php 验证它,它们位于表单上方的同一页面上(而不是在 head 标签中),没有任何乐趣。

*脚本更新数据库 *,ajax 没有从 php 获得回声,这是我想不通的另一件事。所以我有几个问题,不知道,也许页面上有干扰。

我只是想了解它应该如何工作。

当我点击“提交”时 - AJAX 将变量发送到 PHP,然后 PHP 更新数据库并回显字符串,该字符串被传递回 AJAX。但是AJAX看不到它,我在AJAX代码中的错误字符串没有显示,唯一显示的是“发生了一些错误”(我什至在某个阶段将其更改为“成功更新”,因为数据库已更新:))

任何帮助将不胜感激

名为 myaccount.php 的页面

    <?php
// AJAX CALLS THIS CODE TO EXECUTE

if(isset($_POST['p'])||isset($_POST['a1'])||isset($_POST['a2'])||isset($_POST['c'])||isset($_POST['co'])||isset($_POST['con'])||isset($_POST['da1'])||isset($_POST['da2'])||isset($_POST['dc'])||isset($_POST['dco'])||isset($_POST['addcheckbox'])){
    require_once('./includes/mysql_connect.php');

$p = mysql_real_escape_string(trim($_POST['p']));
$a1 = mysql_real_escape_string(trim($_POST['a1']));
$a2 = mysql_real_escape_string(trim($_POST['a2']));
$c = mysql_real_escape_string(trim($_POST['c']));
$co = mysql_real_escape_string(trim($_POST['co']));


$da1 = mysql_real_escape_string(trim($_POST['da1']));
$da2 = mysql_real_escape_string(trim($_POST['da2']));
$dc = mysql_real_escape_string(trim($_POST['dc']));
$dco = mysql_real_escape_string(trim($_POST['dco']));

if(isset($_POST['addcheckbox'])=='ticked'){
    $da1 = mysql_real_escape_string(trim($_POST['a1']));
$da2 = mysql_real_escape_string(trim($_POST['a2']));
$dc = mysql_real_escape_string(trim($_POST['c']));
$dco = mysql_real_escape_string(trim($_POST['co']));
    }


$sql = "UPDATE customers SET address_1='$a1', address_2='$a2', phone='$p', city='$c', county='$co', country='$con', del_ad_1='$da1', del_ad_2='$da2', del_city='$dc', del_county='$dco' WHERE id_cust='$id'";
$query = mysql_query($sql);

if(!$query){

    echo 'success';
    exit();
}else{

    echo 'not_updated';
    exit();
    }  
}


 ?>

这是javascript

    <script type="text/javascript" language="javascript">


    function changeaddress(){


    var p = _("phone").value;
    var a1 = _("address_1").value;
    var a2 = _("address_2").value;
    var c = _("city").value;
    var co = _("county").value;
    var con = _("country").value;

    var da1 = _("deladdress_1").value;
    var da2 = _("deladdress_2").value;
    var dc = _("delcity").value;
    var dco = _("delcounty").value;

//checkbox

    if(document.getElementsById("addcheckbox").checked){
        var da1 = a1;   
        var da2 = a2;
        var dc = c;
        var dco = co;

        }else{
        var da1 = _("deladdress_1").value;
        var da2 = _("deladdress_2").value;
        var dc = _("delcity").value;
        var dco = _("delcounty").value;}


//ajax call

    if((p!=""||p=="") && (a1!="") && (a2!=""||a2=="") && (c!="") && (co!="")){
        //_("changeaddbtn").style.display = "none";
        _("status").innerHTML = 'updating your details ...';

        var ajax = ajaxObj("POST", "/myaccount.php");

        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                var response = ajax.responseText;
                if(response == 'success'){
                    _("changeaddressform").innerHTML = '<h3>Address is updated, check the first tab!!</p>';
                } else if (response == 'not_updated'){
                    _("status").innerHTML = "An error occured , the details were not updated!";
                } else {
                    _("status").innerHTML = "Some error occured!";

                }
            }else{
                _("status").innerHTML = "loading...";}
        }
        ajax.send("p="+p+"&a1="+a1+"&a2="+a2+"&c="+c+"&co="+co+"&da1="+da1+"&da2="+da2+"&dc="+dc+"&dco="+dco)

    ;
            }
    }





</script>

这是帐单部分

                        Address*
                  --------------------
                        <input class="input_field" type="text" size="40" id="address_1" name="address_1" value="<?php echo ''.$get_row['address_1'].''; ?>"/> 

                        Address
                  ----------------------  
                        <input class="input_field" type="text" size="40" id="address_2" name="address_2" value="<?php echo ''.$get_row['address_2'].' '; ?>"/>

                        City/Town*

                        <input class="input_field" type="text" size="40" id="city" name="city" value="<?php  echo ''.$get_row['city'].' '; ?>"/>

                        County*
                   -----------------------
                        <input class="input_field"  type="text" size="40" id="county" name="county" value="<?php  echo ''.$get_row['county'].' '; ?>"/> 

                <p><button id="changeaddbtn" class="submit_btn" onclick="changeaddress()" >Submit Changes</button>

送货

<input type="checkbox" name="addcheckbox" id="addcheckbox" value="ticked"  /> Same as billing </p>

"/>

                        Address
                    -------------------------------
                        <input class="input_field" type="text" size="40" id="deladdress_2" value="<?php echo ''.$get_row['del_ad_2'].' '; ?>"/>

                        City/Town*
                  -------------------------
                        <input class="input_field" type="text" size="40" id="delcity" value="<?php  echo ''.$get_row['del_city'].' '; ?>"/>

                        County*
                  -------------
                        <input class="input_field"  type="text" size="40" id="delcounty" value="<?php  echo ''.$get_row['del_county'].' '; ?>"/>
          -----------------------------------------------
 <p><button id="changeaddbtn" class="submit_btn" onclick="changeaddress()" >Submit Changes</button></p>
                    <p id="status"></p>
                    <p id="status1"></p>
                    <p id="status2"></p>
                    <p id="status3"></p>
4

1 回答 1

0

这是我的答案

function checkCheckbox(){
    if (_("checkbox1").checked == true){
        _("deladdress_1").value = _("address_1").value;
    _("deladdress_2").value = _("address_2").value;
    _("delcity").value = _("city").value;
    _("delcounty").value = _("county").value;
    _("delcountry").value = _("country").value;
    }else{
        _("deladdress_1").value = "";
    _("deladdress_2").value = "";
    _("delcity").value = "";
    _("delcounty").value = "";
    _("delcountry").value = "";

        }
    }
于 2013-11-24T20:12:01.620 回答