dsc7188
2014-08-04 11:25
浏览 31

如何通过AJAX将ID发送到另一个文件并在这样的文件中使用ID

I have an HTML file which contains various fields as follows(I will only show one example since the rest are the same):

<map name="parcerlas">
    <area shape="poly" coords="618,4,618,74,681,76,683,56,684,39,684,27,683,14,682,4" href="#" alt="Parcela 9" title="Parcela 9" onclick="getdata('9');">
</map>

Using the functiong getdata() I retrieve the ID inside and I need to pass this ID using AJAX to another PHP file and use it in the new file. How can I achieve this?

Here is my getdata() function:

<script type="text/javascript">
    function getdata(id){
    alert(id);  
        $.ajax({
           type: "POST",
           url: "activations.php",
           data: 
           {
              id;
           }
    }
</script> 

图片转代码服务由CSDN问答提供 功能建议

我有一个HTML文件,其中包含以下各种字段(我只会显示一个例子,因为其余字段相同 ):

 &lt; map name =“parcerlas”&gt; 
&lt; area shape =“poly”coords =“618,4,618,74,681,76,683,56,684,39,684  ,27,683,14,682,4“href =”#“alt =”Parcela 9“title =”Parcela 9“onclick =”getdata('9');“&gt; 
&lt; / map&gt; 
  <  / pre> 
 
 

使用functiong getdata()我检索里面的 ID ,我需要传递这个 ID AJAX 到另一个PHP文件并在新文件中使用它。 我怎样才能实现这个目标?

这是我的 getdata()函数:

 &lt; script type  =“text / javascript”&gt; 
 function getdata(id){
 alert(id);  
 $ .ajax({
 type:“POST”,
 url:“activations.php”,
 data:
 {
 id; 
} 
} 
&lt; / script&gt; 
    
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drcigvoy48900 2014-08-04 11:27
    已采纳

    You need to specify the key of the data:

    data: { 'id': id }
    

    Also there are syntax errors in your snippet:

    function getdata(id){
        alert(id);  
        $.ajax({
            type: "POST",
            url: "activations.php",
            data: {
                'id': id // < no need for semi-colon, properties are key->value based, 'key': value
            }
        }); // << no closing bracket and semi-colon
    } // << close your function
    

    So you can access it in your activations.php:

    $_POST["id"]
    

    Always check if it is isset or not:

    if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
    

    Answering OP's particular questions:

    You need to open a PDO connection to your SQL server (example with MySQL)

    if (isset($_POST["id"])) $id = $_POST["id"]; else exit;
    
    $db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', 
                  'username', 
                  'password'); // Open a connection to your database
    $q = $db->prepare("SELECT * FROM achievements WHERE id=?"); // Get all achievements where id=$id
    $q->execute(array($id)); // Run the query
    
    $rows = $q->fetchAll(); // Fetch the query, associated array
    
    echo json_encode($rows); // Convert the $rows to json, so javascript can read it
    

    You will have to set the datatype of your ajax request to JSON or parse the data on your own with $.parseJSON(data);

    You can use the returned data with adding a success function to your ajax:

    function getdata(id){
        alert(id);  
        $.ajax({
            type: "POST",
            url: "activations.php",
            data: {
                'id': id // < no need for semi-colon, properties are key->value based, 'key': value
            },
    
            success: function(data) {
                var achievements = $.parseJSON(data);
    
                // use achievements variable
            }
        }); // << no closing bracket and semi-colon
    } // << close your function
    
    点赞 打赏 评论
  • drhqkz3455 2014-08-04 11:29

    Change id; to 'id':id and no semicolon(;)

    点赞 打赏 评论
  • duanqian6982 2014-08-04 11:34

    You have a few typos (no closing brackets on Ajax call, unwanted semi-colon etc), but mainly need to provide the data as a key value pair. Note: the quotes are only required in a key name if it is not a single word (no spaces or hyphen/- etc):

    <script type="text/javascript">
        function getdata(id){
        alert(id);  
            $.ajax({
               type: "POST",
               url: "activations.php",
               data: 
               {
                  id: id
               }
            });
        }
    </script> 
    
    点赞 打赏 评论

相关推荐 更多相似问题