简体   繁体   English

如何使用GET将值从Ajax传递到php

[英]How to pass values from ajax to php using GET

I have this script in ajax and is used to pass the value chosen by the user in the combo box to php. 我在ajax中有此脚本,用于将用户在组合框中选择的值传递给php。 This also handles the submit button. 这也处理提交按钮。

AJAX: AJAX:

<script>    
function loadXMLDoc(usertype) {
    var value = $('#usertype').val();
    var page='checksignup.php?usertype='+usertype;
    document.location.href=page;
}


$( "#register_Chairperson" ).submit(function( event ) {
  var usertype = $('#usertype').val();
  alert(usertype);
  loadXMLDoc(usertype);
  event.preventDefault();
});
</script>

And I'm using this php code to get the value pass by ajax. 而且我正在使用此php代码来获取ajax传递的值。 But my problem is that nothing happens. 但是我的问题是什么也没发生。 My address became only like this: http://localhost/civil-engineering-dept/checksignup.php?usertype=1 . 我的地址变成这样: http://localhost/civil-engineering-dept/checksignup.php?usertype=1 It did not continue in checking for the form for signing up. 它没有继续检查注册表单。 What do you think is the problem? 您认为出了什么问题?

PHP: PHP:

$user = $_GET["usertype"];

And I'm using this php code to get the value pass by ajax. 而且我正在使用此php代码来获取ajax传递的值。

That's not ajax, it looks like an attempt to load the XML in the browser window. 那不是ajax,它看起来像是在浏览器窗口中加载XML的尝试。 To actually do that, change document to window and lose the href . 为此,请将document更改为window并丢失href

window.location=page;

To actually use ajax, you'd use $.ajax or $.get (since you're trying to do a GET); 要实际使用ajax,您可以使用$.ajax$.get (因为您尝试执行GET); something like this: 像这样的东西:

$.get(page)
    .done(function(xml) {
        // Worked, do something with the XML
    })
    .fail(function() {
        // Failed, show/handle the error
    });

try this 尝试这个

$user = $_REQUEST["usertype"]; $ user = $ _REQUEST [“ usertype”];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM