PHP动态选择Onchange不工作

时间:2017-07-27 02:23:50

标签: php jquery select dynamic joomla

我试图进行动态选择,我已经在stackoverflow上尝试了一些示例,但它仍然无法正常工作..

我认为代码很简单..选择汽车的品牌,然后过滤的模型出现..

我使用joomla,jquery和php(所有这些都是最新版本)



<?php
defined('_JEXEC') or die;
$document = JFactory::getDocument();
$document->addScript("/media/jui/js/jquery.min.js");


//CONNECTION ON  BD IS WORKING NORMALLY
$db = JFactory::getDbo();

?>

<script type="text/javascript">
function ajaxModeloRequest(val)
{
 $.ajax({
 type: 'post',
 url: 'veiculomodelo.php',
 data: {
  get_marca:val
 },
 success: function (response) {
  document.getElementById("modelo").innerHTML=response; 
 }
 });
}

</script>


  
<span style="font-size: 15px;    font-weight: 500;    line-height: 18px;    margin-bottom: 8px;    text-transform: uppercase;">Marca</span>
<select id="marca" name="marca" class="inputbox" onchange="ajaxModeloRequest">
    <?php

    
    // Get a new JDatabaseQuery object
    $query = $db->getQuery(true);
    
                    
    
    // construindo query veiculo marca
    $query->select(array('id', 'marca'));
    
    $query->from($db->quoteName('#__veiculo_marca'));
    $query->where($db->quoteName('tipo_fk') .'=' . $db->quote('1'));
    
    // Reset the query using our newly populated query object.
    $db->setQuery($query);
    
    // Returns an indexed array of associated arrays from the table records returned by the query
    $results = $db->loadAssocList();
    
    foreach ($results as $row) :
      echo "<option value='" . $row['id'] . "'>" . $row['marca'] . "</option>";
    endforeach;
    
    
    ?>
</select>

<span style="font-size: 15px;    font-weight: 500;    line-height: 18px;    margin-bottom: 8px;    text-transform: uppercase;">Modelo</span>
<select id="modelo" name="modelo" class="inputbox">

</select> 
&#13;
&#13;
&#13;

这是被调用的代码..文件veiculomodelo.php

&#13;
&#13;
<?php

defined('_JEXEC') or die;


$document = JFactory::getDocument();
$document->addScript("/media/jui/js/jquery.min.js");



if(isset($_POST['get_marca']))
{

    //conectando com o banco de dados para trazer os carros
    $db = JFactory::getDbo();
    
    // Get a new JDatabaseQuery object
    $query = $db->getQuery(true);


    $marca = $_POST['get_marca'];

    // construindo query veiculo modelo
    $query->select(array('id', 'modelo'));
    
    $query->from($db->quoteName('#__veiculo_modelo'));
    $query->where($db->quoteName('marca_fk') .'=' . $marca);
    
    // Reset the query using our newly populated query object.
    $db->setQuery($query);
    
    // Returns an indexed array of associated arrays from the table records returned by the query
    $results = $db->loadAssocList();

 while($row=mysql_fetch_array($find))
 {
  echo "<option>".$row['city']."</option>";
 }
 exit;

/*    
    foreach ($results as $row) :
      echo "<option value='" . $row['id'] . "'>" . $row['modelo'] . "</option>";
    endforeach;
*/
    //exit;
}
?>
&#13;
&#13;
&#13;

那就是..有人可以告诉我为什么它不起作用?

2 个答案:

答案 0 :(得分:1)

There is a javascript issue on the site你的onchange代码没有将值发送给函数。

使用此

<select id="marca" name="marca" class="inputbox" onchange="ajaxModeloRequest(this.value);">

在ajax调用中使用jQuery.ajax而不是$ .ajax

答案 1 :(得分:0)

作为其他人找到此主题的替代方法,您可以使用RSForms Pro,它具有条件字段的内置接口,并允许代码从数据库表中提取:

https://www.rsjoomla.com/support/documentation/rsform-pro/custom-scripting/auto-populate-a-list-from-a-table.html

对于多个过滤器,您可以将提交按钮更改为“下一步”,并将“返回URL”设置为同一页面但具有所选值(即site.com/myform?model={model:value}

或者您在javascript中更改set onchange以使用变量重新加载窗口。

然后为这些做一个$ _GET,并将它们包含在数据库查询中,以便随后设置下拉列表。

我喜欢这种方式,因为它与Joomla更好地集成了电子邮件功能,菜单项和模块,还有很多其他功能。