drh47606 2016-09-08 18:56
浏览 63

json_encode的Php echo返回值但在javascript(ajax)中为null

I have a registration form which i would like to get the last id from the php for java to be used inside window.location. I'm using json_encode to encode the value from php to javascript. Before the page redirects i can see the echoed json_encode with the last id but the id variable used inside window.location returns null.

Any help would really be appreciated. Please forgive any amateur mistakes since I'm not a javascript(ajax expert).

<?php

if(isset($_POST['n'])){
$user = new User();
$user->name = trim($_POST['n']);
$user->email = trim($_POST['e']);
$user->password = trim($_POST['p']);
$user->name_of_institution = trim($_POST['ni']);
$user->name_of_institution_db = strtolower(str_replace(" ", "_", (trim($_POST['ni']))));
$user->type_of_school = trim($_POST['st']);
$user->country = trim($_POST['c']);
$user->region = trim($_POST['r']);
$user->town = trim($_POST['t']);
$user->zip_code = trim($_POST['z']);
$user->postal_address = trim($_POST['pa']);
$user->street_address = trim($_POST['sa']);
$user->school_code = trim($_POST['sc']);
$user->waec_code = trim($_POST['wc']);
$user->doe = trim($_POST['d']);
$user->registration_date = strftime("%Y-%m-%d %H:%M:%S", time());
$user->uid = crypt(uniqid(rand(),1));

//ERROR HANDLING
if($user->name == "" || $user->email == "" || $user->password == "" || $user->name_of_institution == "" || $user->type_of_school == "" || $user->country == "" || $user->region == "" || $user->town == "" || $user->zip_code == "" || $user->postal_address == "" || $user->street_address == ""|| $user->school_code == "" || $user->waec_code == "" || $user->doe == "" ){
    echo "The submission form is missing some values. Kindly complete the form and try again";
    exit();
} else {
    if($user->save() && $user->create_school_table() && $user->create_comment_table() && $user->create_course_table() && $user->create_teacher_table() && $user->create_score_table() && $user->create_status_table() && $user->create_message_table() && $user->create_reply_table() && $user->create_level_table() && $user->insert_subjects_into_course_table() && $user->insert_levels_into_level_table() && $user->create_timeline_table()) {

    //coment saved
    //No message needed; seeing the comment again is proof enough

    //Important! You could jsut let the page render from here
    //But then if the page is reloaded, the form will try
    //to resubmit the comment. So redirect instead:
    if (!file_exists(SITE_ROOT .DS. 'public_html' .DS. 'images' .DS. $user->id)) {
        mkdir (SITE_ROOT .DS. 'public_html' .DS. 'images' .DS. $user->id, 0777);
    }

    echo "Signup_Success";
    $ide = $user->id;
    $id = utf8_encode($ide);
    echo json_encode($id);
    //echo $id;
    exit();
    //$session->message("Successful Registration.");
    //redirect_to("logo_upload.php?id={$user->id}&name_of_institution={$user->name_of_institution}");

}  
}  
}

?>


  <script>
      function restrict(elem){
          var tf = _(elem);
          var rx = new RegExp;
          if(elem == "email"){
              rx = /[' "]/gi;
          } else if(elem == "username"){
              rx = /[^a-z0-9]/gi;
          } else if(elem == "school_code"){
              rx = /[^0-9]/gi;
          } 
          tf.value = tf.value.replace(rx, "");
      }

      function emptyElement(x){
          _(x).innerHTML = "";
      }


      function checkusername(){
          var n = _("name").value;
          if(n != ""){
              _("usernamestatus").innerHTML = 'Checking...';
              var ajax = ajaxObj("POST", "ajax_form_check.php", true);
              ajax.onreadystatechange = function() {
                  if(ajaxReturn(ajax) == true) {
                      _("usernamestatus").innerHTML = ajax.responseText;
                  }
              }                  
              ajax.send("name2check="+n);
          }
      }

      function matchpassword(){
          var p1 = _("password").value;
          var p2 = _("rpassword").value;
          if((p1 != p2) || (p2 != p1)){
              _("passwordstatus").innerHTML = '<p class=\"btn btn-danger text-center col-sm-6 col-sm-offset-3\">Confirmation failed. Please recheck.</p>';
          }else {
              _("passwordstatus").innerHTML = '';
          }
      }

      function signup(){
          var n = _("name").value;
          var e = _("email").value;
          var p1 = _("password").value;
          var p2 = _("rpassword").value;
          var ni = _("name_of_institution").value;
          var st = _("type_of_school").value;
          var c = _("country").value;
          var r = _("region").value;
          var t = _("town").value;
          var z = _("zip_code").value;
          var pa = _("postal_address").value;
          var sa = _("street_address").value;
          var sc = _("school_code").value;
          var wc = _("waec_code").value;
          var d = _("doe").value;
          var status = _("status");

          if(n == "" || e == "" || p1 == "" || p2 == "" || ni == "" || st == "" || c == "" || r == "" || t == "" || z == "" || pa == "" || sa == ""|| sc == "" || wc == "" || d == ""){
              window.scrollTo(0,0);
              status.innerHTML = "<p class=\"btn btn-danger text-center col-sm-6 col-sm-offset-3\">Please complete the form. There seems to be some empty spaces. Thank you.</p><br>";
          } else if(p1 != p2){
              window.scrollTo(0,0);
              status.innerHTML = "<p class=\"btn btn-danger text-center col-sm-6 col-sm-offset-3\">Your password fields do not match</p><br>";
              //} else if( _("terms").style.display == "none"){
              //status.innerHTML = "Please view the terms of use";
          } else {
              _("signupbtn").style.display = "none";
              status.innerHTML = 'Please wait ...';
              var ajax = ajaxObj("POST", "regform.php");
              ajax.onreadystatechange = function() {
                  if(ajaxReturn(ajax) == true) {
                    //var id = <?php echo json_encode($id); ?>;
                      if(ajax.responseText == "Signup_Success"){
                          status.innerHTML = ajax.responseText;
                          _("signupbtn").style.display = "block";
                      } else {
                      //var id = <?php echo json_encode($id); ?>;
                      var id = JSON.parse( '<?php echo json_encode($id); ?>' );

                          window.location ="logo_upload.php?id="+id+"&name_of_institution="+ni;
                      }
                  }
              }
              ajax.send("n="+n+"&e="+e+"&p="+p1+"&ni="+ni+"&st="+st+"&c="+c+"&r="+r+"&t="+t+"&z="+z+"&pa="+pa+"&sa="+sa+"&sc="+sc+"&wc="+wc+"&d="+d);
          }
      }

  </script>

The php and ajax code are all on the same page regform.php. Will seperating the codes help?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)