简体   繁体   English

PHP简单HTML DOM和下拉菜单元素选择选项

[英]PHP Simple HTML DOM and Dropdown Element Selected Option

I'm generating dropdown inputs with php simple html dom. 我正在使用php简单html dom生成下拉输入。 I have some values from database and according them i will add 'selected' value into my select->option dom elements. 我有一些来自数据库的值,根据它们我将在我的select-> option dom元素中添加“ selected”值。

<select name="myDropDown" id="someID">
   <option value="someValue1">Value1Value</option>
   <option value="someValue2">Value2Value</option>
   <option value="someValue3">Value3Value</option>
</select>

this is the default one. 这是默认值。 i would like to add value like this, check on option 2 : 我想像这样增加价值,请检查选项2:

<select name="myDropDown" id="someID">
   <option value="someValue1">Value1Value</option>
   <option value="someValue2" selected>Value2Value</option>
   <option value="someValue3">Value3Value</option>
</select>

and while doing that i want to use my plugin. 在这样做的同时,我想使用我的插件。

<?php    
    $html = new simple_html_dom();
    $html->load('
           <select name="myDropDown" id="someID">
             <option value="someValue1">Value1Value</option>
             <option value="someValue2">Value2Value</option>
             <option value="someValue3">Value3Value</option>
           </select>
          ');
    echo $html;
?>

Everything works nicely until here. 一切正常,直到这里。 Yeah now i need to insert selected into my second option. 是的,现在我需要在我的第二个选项中插入selected I don't know how to do this with PHP Simple HTML DOM, or i'm missing something in the documentation : http://simplehtmldom.sourceforge.net/manual.htm#section_access 我不知道如何使用PHP Simple HTML DOM执行此操作,或者我在文档中缺少某些内容: http : //simplehtmldom.sourceforge.net/manual.htm#section_access

What i have tried so far and got many errors in my php section : 我到目前为止已经尝试了什么,并且在我的php部分中遇到了很多错误:

<?php
$html = new simple_html_dom();
   $html->load('
              <select name="myDropDown" id="someID">
                 <option value="someValue1">Value1Value</option>
                 <option value="someValue2">Value2Value</option>
                 <option value="someValue3">Value3Value</option>
              </select>
              ');
   //here's where i'm trying to reach the child nodes :    
   $ret = $html->getElementById('someID');
   $ret->children(2)->GOTTADOSOMETHINGHEREANDIREALLYDUNNO;
   echo $html;
?>

By the way, if you offer another easy way to do this, i'd appreciate. 顺便说一句,如果您提供另一种简单的方法来执行此操作,我将不胜感激。

Thank you in advance ! 先感谢您 !

Why you use simple_html_dom and not simply a template file (with PHP)? 为什么要使用simple_html_dom而不是仅使用模板文件(使用PHP)?

<select name="test" <?php if($selected){echo 'selected'}?> />

Or even better Smarty. 甚至更好的Smarty。

<select name="test" {if $selected}selected{/if} />

Simple_html_dom is a class for parsing complex html documents like exchanging image urls or something like that. Simple_html_dom是用于解析复杂的html文档(例如交换图像url等)的类。 I cant' see any reason why you need to use this class. 我看不到任何需要使用此类的原因。

通过最少的研究,您可以自己解决这个问题。

$ret->children(2)->selected = true;

for getting Dropdown Element Selected Option with Simple HTML DOM 用于使用简单HTML DOM获取下拉列表元素选定选项
just try this simple and easy method 只需尝试这种简单的方法

 $element =  $html->find('#selectIDGoesHere',0)->find('option');     
        foreach($element as $elemen) {         
            echo "Display text:".($elemen->plaintext)."<br>"; 
            echo "value:".($elemen->value)."<br>";
        } 

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

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