I am trying to get the information from a html table into an php array. It is very easy to do with the following method:
function getdata($table)
{
$DOM = new DOMDocument;
$DOM->loadHTML($table);
$items = $DOM->getElementsByTagName('tr');
function tdrows($elements)
{
$str = "";
foreach ($elements as $element)
{
$str .= $element->nodeValue . ", ";
}
return $str;
}
foreach ($items as $node)
{
echo tdrows($node->childNodes) . "<br />";
}
}
The problem I am now facing is the content of the table has html inputs and I want just the value of those inputs. The table is of the form:
<table>
<tr><td><input type="text" /></td><td><input type="text" /></td><td><div class="add">add</div></td></tr>
</table>
Will I be able to modify the current function to accomplish this or should I try another approach
As PHP is server-side, you will not be able to get the value of the input unless you submit this inputted value to the server, for example using post or get when the form is submitted. You should use Javascript if you want the value of the input without having to submit the form. If this doesn't answer your question please try to ask it more clearly : )
Related
I tried updating a meta value from wordpress frontend using the following code:
function code_form()
{
$content = '';
$content .= '<h2> code </h2>';
$content .='<form method="post">';
$content .='<input type = "text" name="code"/>';
$content .= '<br /><input type="submit" name="Activate" value="continue">';
$content .= '</form>';
return $content;
}
add_shortcode('form1','code_form');
function a()
{
if(isset($_POST['Activate']))
{
$input = sanitize_text_field($_POST['code']);
$current_user_id = get_current_user_id();
update_user_meta($current_user_id, 'metakey_' ,$input);
echo "<script>alert('success')</script>";
}
}
add_action('wp_head','a');
And the input value should be something like:
a:1:{i:0;s:1:"1";}
However, when I check phpmyadmin for the updated result, something different is stored, such as
s:1"a:1:{i:0;s:1:"8";}"
How should I change my code to make the stored value exactly same as my input?
I tried deleting the metakey and update the value, but still doesn't work.
Your input data is serialized twice that's why it happens, try to pass an array
$input = explode(',' , $input);
Here is the same question asked
I need one help.I have multiple text field inside a div and i need to count those using PHP.I am explaining my code below.
<div class="questionshowp">
<input name="optional_0_0_ans" id="optional_0_0_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_1_ans" id="optional_0_1_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
<input name="optional_0_2_ans" id="optional_0_2_ans" class="form-control firstsec" placeholder="Text, Image URL, or LaTeX" value="" type="text">
</div>
<?php
?>
Here 3 input fields are present inside a div.Here i need to echo how many nos of field is present using PHP.Please help me.
I am assuming that the HTML is not built/rendered using PHP, so then you can use the PHP built-in DOM parser.
<?php
//** Load the HTML **//
//$html = file_get_contents('http://www.url.com'); //Load the HTML from external webpage.
$html = file_get_contents(__FILE__); //Load the HTML from the current webpage.
//** Load HTML contents into DOM tree **//
$dom = new DOMDocument();
$dom->loadHTML($html);
//** Initialize DOM Parser **//
$finder = new DomXPath($dom);
//** Parse DOM for all occurences of <input> with parent <div> with class ="questionshowp' **//
$inputs = $finder->query("/html/body/div[#class='questionshowp']/input");
//** Determine how many input fields have type='text' **//
$count = 0;
foreach ($inputs as $input) {
if ($input->getAttribute('type') === 'text') {
$count++;
}
}
//** Print Results! **//
echo "Number of <input> fields where #type = 'text' is -> " .$count ."\n";
REFERENCE
http://php.net/manual/en/book.dom.php
Keep a flag $count and increment inside the for loop
$count = 0;
for loop {
$count++
}
echo $count;
or
$count = count($listValue);
I want to access a custom attribute that I added to some elements in an HTML file, here's an example of the littleBox="somevalue" attribute
<div id="someId" littleBox="someValue">inner text</div>
The Following doesn't work:
foreach($html->find('div') as $element){
echo $element;
if(isset($element->type)){
echo $element->littleBox;
}
}
I saw an article with a similar problem, but I couldn't replicate it for some reason. Here is what I tried:
function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
echo $var1[0];
}
else
return false;
}
When ever I call the retrieveValue() function, nothing happens. Is $element (in the first PHP example above) not a string? I don't know if I missed something but it's not returning anything.
Here's the script in it's entirety:
<?php
require("../../simplehtmldom/simple_html_dom.php");
if (isset($_POST['submit'])){
$html = file_get_html($_POST['webURL']);
// Find all images
foreach($html->find('div') as $element){
echo $element;
if(isset($element->type)!= false){
echo retrieveValue($element);
}
}
}
function retrieveValue($str){
if (stripos($str, 'littleBox')){//check if element has it
$var=preg_split("/littleBox=\"/",$str);
//echo $var[1];
$var1=preg_split("/\"/",$var[1]);
return $var1[0];
}
else
return false;
}
?>
<form method="post">
Website URL<input type="text" name="webURL">
<br />
<input type="submit" name="submit">
</form>
Have you tried:
$html->getElementById("someId")->getAttribute('littleBox');
You could also use SimpleXML:
$html = '<div id="someId" littleBox="someValue">inner text</div>';
$dom = new DOMDocument;
$dom->loadXML($html);
$div = simplexml_import_dom($dom);
echo $div->attributes()->littleBox;
I would advice against using regex to parse html but shouldn't this part be like this:
$str = $html->getElementById("someId")->outertext;
$var = preg_split('/littleBox=\"/', $str);
$var1 = preg_split('/\"/',$var[1]);
echo $var1[0];
Also see this answer https://stackoverflow.com/a/8851091/1059001
See that http://code.google.com/p/phpquery/ it's like jQuery but on php. Very strong library.
I am using CURL to fetch form and store it in a field
..,
$str = curl_exec($ch);
The $str HTML has a textarea as follows
<td class="fntc">
Description
</td>
<td class="ffc">
<textarea name="descri" rows="6" class="emf" maxlength="128000">fictional.</textarea>
</td>
</tr>
Now I am trying to use a dom to fetch this area and was unsuccessful
$dom = new DOMDocument;
$dom->loadHTML($str);
// Get all the textarea field nodes
$inputs = $dom->getElementsByTagName('textarea');
// Iterate over the input fields and save the values we want to an array
foreach ($inputs as $input) {
$name = $input->getAttribute('name');
$val = $input->getAttribute('value');
$field_vals[$name] = $val;
}
But i am unable to get the value.Is there anything i am doing wrong here?
Since a <textarea> contains text inside the tag, rather than in a value attribute, you may access it with nodeValue:
$val = $input->nodeValue;
Update
Ok, I've verified this now:
$d = new DOMDocument();
$d->loadHTML("<html><head></head><body><textarea>textarea contents</textarea></body></html>");
$t = $d->getElementsByTagName("textarea");
foreach ($t as $tx) {
echo $tx->nodeValue;
}
// Prints
// textarea contents
I'm creating a "Madlibs" page where visitors can create funny story things online. The original files are in XML format with the blanks enclosed in XML tags
(Such as blablabla <PluralNoun></PluralNoun> blablabla <Verb></Verb> ).
The form data is created using XSL and the results are saved using a $_POST array. How do I post the $_POST array between the matching XML tags and then display the result to the page? I'm sure it uses a "foreach" statement, but I'm just not familiar enough with PHP to figure out what functions to use. Any help would be great.
Thanks,
E
I'm not sure if I understood your problem quite well, but I think this might help:
// mocking some $_POST variables
$_POST['Verb'] = 'spam';
$_POST['PluralNoun'] = 'eggs';
// original template with blanks (should be loaded from a valid XML file)
$xml = 'blablabla <PluralNoun></PluralNoun> blablabla <Verb></Verb>';
$valid_xml = '<?xml version="1.0"?><xml>' . $xml . '</xml>';
$doc = DOMDocument::loadXML($valid_xml, LIBXML_NOERROR);
if ($doc !== FALSE) {
$text = ''; // used to accumulate output while walking XML tree
foreach ($doc->documentElement->childNodes as $child) {
if ($child->nodeType == XML_TEXT_NODE) { // keep text nodes
$text .= $child->wholeText;
} else if (array_key_exists($child->tagName, $_POST)) {
// replace nodes whose tag matches a POST variable
$text .= $_POST[$child->tagName];
} else { // keep other nodes
$text .= $doc->saveXML($child);
}
}
echo $text . "\n";
} else {
echo "Failed to parse XML\n";
}
Here is PHP foreach syntax. Hope it helps
$arr = array('fruit1' => 'apple', 'fruit2' => 'orange');
foreach ($arr as $key => $val) {
echo "$key = $val\n";
}
and here is the code to loop thru your $_POST variables:
foreach ($_POST as $key => $val) {
echo "$key = $val\n";
// then you can fill each POST var to your XML
// maybe you want to use PHP str_replace function too
}