I'm using simple html dom in php to extract a table depending on its id. I have done this without any issues when the id doesn't involve any characters like hyphens(-). I suspect it is due to a hyphen because I used the same code with an id with no hyphens and no trouble receiving the data. The data I want to extract is also in a tab that is hidden, does this effect the process?
Here is my code
<?php
include('simple_html_dom.php');
//Insert the url you want to extract data from
$html = file_get_html('http://espnfc.com/team/_/id/359/arsenal?cc=5739');
$i = 0;
$dataInTable = true;
while($dataInTable){
if($html->find('div[id=ui-tabs-1] table tbody', 0)->children(0)->children($i)){
for($j=0;$j<3;$j++){
if($html->find('div[id=ui-tabs-1] table tbody', 0)->children(0)->children($i)->children($j)){
$gk[] = $html->find('div[id=ui-tabs-1] table tbody', 0)->children(0)->children($i)->children($j)->plaintext;
}else{
$dataInTable = false;
}
}
//else if nothing is in the next cell return false.
}else{
$dataInTable = false;
}
$i+=2;
}
var_dump($gk);
?>
Here is the HTML content
When you take a look at the source(not via dev-tools, use browser->viewsource) of http://espnfc.com/team/_/id/359/arsenal?cc=5739 you'll not see anything with the ID ui-tabs-1
This element has been created via javascript(I guess jQueryUI-tabs)
simple_html_dom parses HTML but did not evaluate javascript, so the answer is:
You can't select this element
Related
I have this problem, I'm using PHP to create an HTML document, creating everything with echo, my problem is that I'm creating a select, but it must be filled when the index of the other one changes, filling in doc creation is easy, but how can I fill it using PHP when selecting something in the other select?
$html_ciudad = '';
$file = $DOCUMENT_ROOT. "newContact.php";
$dom = new DOMDocument;
$dom->loadHTMLFile($file);
$div = $dom->getElementById("City");
foreach($ciudades->getCiudad($id) as $ciu){
$newDiv = $dom->createElement('option');
$newDiv->setIdAttribute($ciu["IdCd"]);
$newDiv->nodeValue(utf8_encode($ciu["Cd"]));
$div->appendChild($newDiv);
$html_ciudad .=
'<option id="'.$ciu["IdCd"].'" value="">'.utf8_encode($ciu["Cd"]).'</option>';
}
I tried that way, everything working, but not showing result, If someone knows how to do this, even if the code is completely different, is welcome haha thanks.
So if i understood it well, if your select value change, you should load from another page with ajax another select...This code may help:
in your main page:
$('select').on('change', function() {
if(this.value==1){
$("#div1").load("another_select.php");
}
})
</script>
in another_select.php page:
echo "<select>";
...
echo "</select>";
I'm working with Selectize.js currently and am stuck on a problem relating to HTML entities not displaying correctly in Selectize fields, and therefore failing checks after submission. We use PHP on the backend and run every Selectize option through htmlspecialchars($option);. We then export the PHP array to a JSON array in preparation for adding it to the Selectize script/field.
//Array of options
$options = getOptions();
$optionsArray = array();
foreach($options as $item) {
$optionsArray[] = array('text' => htmlspecialchars($item), 'value' => htmlspecialchars($item));
}
//Create html script to run selectize initialization
$htmlcode = "<script>";
...
$htmlcode .= "var optionList = " . json_encode($"optionsArray") . ";";
$htmlcode .= "$('#selector').selectize({ options: optionList });";
...
$htmlcode .= "</script>";
The point here is to protect against malicious entries by encoding. This typically wouldn't be a problem, as browsers will detect the entities and convert them automatically (ie. will detect & and display as &. However, that is NOT happening inside the selectize. As you can see in the image below, outputting an option with an ampersand displays correctly on the page, but it doesn't display accurately in the Selectize field.
Example:
The problem then becomes larger, as we are comparing the selected item against the database after submission, to ensure that the selected entry actually exists. If the entry testOption&stuff actually exists, the form submission will check testOption&stuff against it, and will obviously fail the check.
//For simplicity, checking against static option
var $selectedOption = $_POST["options"];
if ($selectedOption == "testOption&stuff") {
//Do success
} else {
//Do failure
}
How can I resolve this and make it so that the Selectize field will store and display the correct entry (ie. &)?
Thanks!
I have an order form (table) that I need to be sent to an email address.
I understand the basics of how to send an email in PHP, but not the specifics
of this situation (taking the information of the cookie array + form elements and put them in body of the PHP mailer)
I am using this link's answer to store the order form as an array in a cookie
Then I use this javascript to create the table in HTML, inside the #catalog div. The important part is the for loop that makes the table. Using list.items(); one can list the "cookie array" as an array
function loopArrayMail() {
if ($.cookie('productWishlist') == null) {
html = ""; $('#catalog').html(html);
} else {
var cookie = $.cookie("productWishlist"); var items = cookie ? cookie.split(/,/) : new Array();
var html = "<table><tr><th>Product</th><th># to order</th></tr>";
for(var i=0;i<items.length;i++){ html += "<tr><td width='450'>"+items[i]+"</td><td><input type='text' name='numberOfItems' /></td></tr>"; }
html += "</table>"; $('#catalog').html(html);
}}
There is a text input next to each item in the order form for the user to input the # of items they want.
How would I send the contents of the table and each form input as an email in PHP?
My guess is that I'd have to take the cookie array, and with each iteration have an array of all the text inputs and use foreach so that they are together in the email (unsure of how to do this exactly). It is also important that there can be any number of text inputs as the number of items on the order form will increase/decrease.
Here is an example of using foreach to get multiple input boxes, but how might I combine the array from my cookie with the text inputs?
Better you send only plain HTML in the email without any javascript because you won't have any javascript in the email program nor you have any cookies.
No idea who told you you could use that, but Email programs are not Browsers.
However if you send a link via email, any user can then open it in a full-featured internet browser so this might be what you're looking for.
Netscape Communicator 4 had support for javascript in emails, no idea about the cookies.
First you must give the form elements a name followed by [] indicating that it is an array, when you create the form elements in the loopArrayMail() function.
for(var i=0;i<items.length;i++){
html += "<tr><td width='450'>"+items[i]+"</td><td><input type='text' name='ordernum[]' size='8' /></td></tr>";
}
Since the cookie is a string you must first explode the string based on the delimiter which is a comma. Place the two arrays into variables. Then use a for loop to go through all of the array.
function explode_trim($str, $delimiter = ',') {
if ( is_string($delimiter) ) {
$str = trim(preg_replace('|\\s*(?:' . preg_quote($delimiter) . ')\\s*|', $delimiter, $str));
return explode($delimiter, $str);
}
return $str; }
$orderCookie = explode_trim($_COOKIE['productWishlist']);
$orderNum = $_POST['ordernum'];
for ( $i = 0; $i < count($orderCookie); $i++) {
echo $orderCookie[$i] . ' = ' . $orderNum[$i] . '<br />';
}
UPDATED
I'm creating a codeigniter callback for validating an input where users enter programming tags for example php, js, jquery. Values are separated by commas.
I want to show a message if you enter duplicate tags for example php, jquery, php, js where php would be the duplicate.
First in my controller I set the validation rules for the 'user_tags` input
$this->form_validation->set_rules('user_tags', 'User Tags', 'callback_user_tags_dublicates', 'trim|xss_clean|max_length[100]|regex_match[/^[a-z,0-9+# ]+$/i]');
Then the callback
<?php function user_tags_dublicates($str)
{
$val = $str; //the input value (all the CSV)
$tags = str_getcsv($val); //creates an array of the CSV
if(count($tags) != count(array_unique($tags))) //if array not equal to unique array it contains duplicates
{
$this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
return FALSE;
}
else
{
return TRUE;
}
} ?>
and finally in the view I show my error.
<?php echo form_error('user_tags'); ?>
When I enter duplicate tags I get
Unable to access an error message corresponding to your field name.
I'm not sure what I'm doing wrong. I tested the function in a static page without validation rules and it works.
set your error message for user_tags inside your user_tags_dublicates() function
$this->form_validation->set_message('user_tags', 'The %s field can not have duplicate tags.');
This might sound stoopid but have you checked:
$tags = str_getcsv($val); //creates an array of the CSV
actually returns the tags properly?
I have form (on my own blog/cms install which i want to play with a bit) with hidden value which i want to extract. Problem is that there are 2 forms on that page, each with that hidden field with value. On each form field name is the same, only hidden value differs. Something like this:
<input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>
Both look the same in html source. So, to help myself i opened php file with this page, edited it and added some random words before field that i need. So now one field (the one that i don't want) is like in above code but field i need is like this:
mywordshere <input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>
How do i extract value from field i need (with mywordshere before its code) if i have my page's html source in php variable (grabbed with libcurl)?
An example using DOMDocument
<?php
$html = <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<body>
<input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere">
</body>
</html>
HTML;
$doc = new DOMDocument();
$doc->validateOnParse = true;
$doc->loadHTML( $html );
$node = $doc->getElementById( '_hiddenname' );
echo $node->getAttribute( 'value' );
?>
Note: your HTML string must have a DOCTYPE defined for this to work.
Assumably the two forms have different names, correct? So if you parse your scraped text with something DOM aware, you should be able to choose your input field by searching for it in its parent form.
The fact that you have two input fields named the same, and with the same id, is the real problem. The id attribute for HTML elements is supposed to be unique on a given page, and if it was, you could do this easily with a DOM parser. Example:
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$inputs = $dom->getElementsByTagName('input');
foreach ($inputs as $i)
{
if ($i->getAttribute('id') == 'targetId') {
//do some stuff
}
}
Since you can't take that approach, and you've marked your input with a string that you can identify, I would use a combination of string functions:
$str = 'mywordshere <input type="hidden" id="_hiddenname" name="_hiddenname" value="valuehere"/>';
$pos = strpos($str,'mywordshere');
if ($pos !== false) {
$valuePos = strpos($str,'value=',$pos);
if ($valuePos !== false) {
//get text starting from the 'value=' portion of the string
$str = substr($str,$valuePos);
$arr = explode('"',$str);
//value will be in $arr[1]
echo $arr[1];
}
}
I would strongly recommend you re-work your element IDs however, and use the DOM approach.
The value will be available in either $_GET["_hiddenname"] or $_POST["_hiddenname"], depending on which method you are using. Which one you get will depend on which form is doing the submitting.
If you have two fields which are named the same within the same form, you have a bigger problem.