简体   繁体   English

如何在ajax函数中调用php文件?

[英]how to call php file in ajax function?

I've got this problem that I can't solve. 我遇到了我无法解决的问题。 Partly because I can't explain it with the right terms. 部分原因是我无法用正确的术语进行解释。 I'm new to this so sorry for this clumsy question. 我对此很陌生,所以很抱歉这个笨拙的问题。

Below you can see an overview of my goal.I tried to get all category id's in a dynamic dropdown list ,the second dropdown list is based on the first dropdown list selection for this i find some code its working fine in local but i tried inside magento its not working Why ? 在下面您可以看到我的目标概述。我试图在动态下拉列表中获取所有类别ID,第二个下拉列表基于第一个下拉列表选择,我在本地找到了一些可以正常工作的代码,但是我尝试在内部magento其不起作用为什么?

Here is my code 这是我的代码

<tr>
                <td class="tdpadfirst">
                <label for="category" class="rightgap"><?php echo Mage::helper('marketplacepartner')->__('Product Category') ?>:</label>
                <span class="required starimp">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                    </td>

                <td class="tdpadfirst">
                <select id="category" class="myinput-text required-entry widthinput" name="category" onChange="updateCategory(this.value)">
                <option value="">--Select Categories--</option>
                <?php
                include('db.php');
                $sql=mysql_query("select entity_id from catalog_category_entity where level='2';");
                while($row=mysql_fetch_array($sql)) {
                $id=$row['entity_id'];
                $data=$row['parent_id'];?>
                <!--echo '<option value="'.$entity_id.'">'.$parent_id.'</option>';-->
                <option value="<?php echo $id ?>"><?php echo $id ?></option>
                <?php  } ?>
                </select>
                <select name="city" class="city">
                <option selected="selected">--Select subcategory--</option>
                </select></td></tr>

<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type="text/javascript">
$(document).ready(function() {
   $(".country").change(function() {
    var id=$(this).val();
    var dataString = 'id='+ id;
    $.ajax ({
    type: "POST",
    url: "ajax_city.php",
    data: dataString,
    cache: false,
    success: function(html) {
       $(".city").html(html);
    }
    });
});
});

ajax_city.php is : ajax_city.php是:

   <?php
include('db.php');
if($_POST['id']) {
   $id=$_POST['id'];
    $sql=mysql_query("select entity_id from catalog_category_entity where parent_id='$id'");
    while($row=mysql_fetch_array($sql)) {
        $id=$row['entity_id'];
        $data=$row['parent_id'];
        echo '<option value="'.$data.'">'.$id.'</option>';
    }
}

?> ?>

And db.php is : db.php是:

   <?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "admin";
$mysql_database = "magento16";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");

?> ?>

Could use specify what you mean by "its working fine in local but i tried inside magento its not working?" 可以用“它在本地工作正常,但我尝试在magento内部工作不正常吗”来指定您的意思?

I'm not 100% sure if you have done this based on your question. 我不确定100%是否已根据您的问题进行了此操作。 Try replacing the php code in ajax_city.php with just html you might be able to isolate the issue. 尝试仅用html替换ajax_city.php中的php代码,您也许可以找出问题所在。

You may need to allow the cross domian access depending on your setup. 您可能需要根据设置来允许跨域访问。 Try adding this code to your ajax_city.php page. 尝试将此代码添加到您的ajax_city.php页面。

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');  

On an unrelated note, the way you have written your ajax_city.php file you have left yourself vulnerable to SQL injection. 无关紧要的是,编写ajax_city.php文件的方式使自己容易受到SQL注入的攻击。 Assuming your "$id" value in your sql is supposed to be an integer you can simply force the typecasting to prevent the sql injection. 假设您在sql中的“ $ id”值应该是整数,则可以简单地强制类型转换来防止sql注入。

...entity_id from catalog_category_entity where parent_id='$id'");

Change: 更改:

$id=$_POST['id'];

To: 至:

$id=intval($_POST['id']); $ id = intval($ _ POST ['id']);

Hope this helps! 希望这可以帮助!

Are you sure the path to ajax file is correct, the way i see magento has a base .htaccess. 您确定ajax文件的路径正确吗,我认为magento具有基本的.htaccess。 Check if the ajax file you are calling is actually being called by checking NET requests sent in your browser, probably using firebug in firefox or chromes default debugger. 通过检查浏览器中发送的NET请求,检查实际上是否正在调用您正在调用的ajax文件,可能是在firefox或chromes默认调试器中使用了firebug。 If its working fine outside magento and path to file is still correct and data is not showing up try making up a simple module in magento by using mage::catalog_category Model. 如果其在magento外部的正常工作和文件路径仍然正确并且数据未显示,请尝试使用mage :: catalog_category模型在magento中构建一个简单模块。 I hope it may help.. :) 我希望它可以帮助.. :)

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

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