json_encode的Php echo在javascript(ajax)

时间:2016-09-08 18:56:27

标签: javascript php json ajax

我有一个注册表单,我想从php中获取最后一个id,以便在window.location中使用java。我正在使用json_encode将值从php编码为javascript。在页面重定向之前,我可以看到带有最后一个id的回显json_encode,但是window.location中使用的id变量返回null。

真的很感激任何帮助。请原谅任何业余错误,因为我不是javascript(ajax专家)。

<?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>

php和ajax代码都在同一页面regform.php上。分离代码会有帮助吗?

0 个答案:

没有答案