I want to extract and echo Attribute Values from both form and input fields from a given HTML. I found that its possible with DOMDocument.
One specific input needs to be grouped by a <div style="position: absolute; left: -5000px;"></div>
I thought that I can either search for if the parent element of the input has this style attributes or if the name of the input field is similiar to b_7e0d9965bedeea1cc5f7217a9_4685998a30. But I have no idea how to do that.
This is the code:
$html = $theme['html']; // From a text input field
$dom = new DOMDocument();
if (#$dom->loadHTML($html)) {
$forms = $dom->getelementsbytagname('form');
foreach ($forms as $form) {
$form_action = $form->getAttribute('action');
$form_method = $form->getAttribute('method');
$form_id = $form->getAttribute('id');
$form_name = $form->getAttribute('name');
$form_class = $form->getAttribute('class');
$form_target = $form->getAttribute('target');
echo '<form';
if (!empty($form_action)) { echo ' action="'. $form_action .'"'; }
if (!empty($form_method)) { echo ' method="'. $form_method .'"'; }
if (!empty($form_id)) { echo ' id="'. $form_id .'"'; }
if (!empty($form_name)) { echo ' name="'. $form_name .'"'; }
if (!empty($form_class)) { echo ' class="'. $form_class .'"'; }
if (!empty($form_target)) { echo ' target="'. $form_target .'"'; }
echo '>';
$inputs = $dom->getelementsbytagname('input');
foreach ($inputs as $input) {
$input_name = $input->getAttribute('name');
$input_value = $input->getAttribute('value');
$input_id = $input->getAttribute('id');
$input_type = $input->getAttribute('type');
$input_class = $input->getAttribute('class');
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}
echo '</form>';
}
}
Some Background Info: I want the user to copy and paste his Email Form Code into a Textbox. Then I want to extract the attributes of the Input Fields to use them inside my Template.
Here are the few things wrong with your syntax
$html = $theme['html']; // Make sure $html is a String
$dom = new DOMDocument();
//if (#$dom->loadHTML($html)) {
if ($dom->loadHTML($html)) { //remove #
//$forms = $dom->getelementsbytagname('form');
$forms = $dom->getElementsByTagName("form");
Make these changes and check again. Hope it should work then
To get the parent node of each input field, you can use
$parentOfInput = $input->parentNode();
$parentAttribute = $parentOfInput->getAttribute('style');
To group each form in a div, try doing this:
//echo '<form';
echo '<div style="position: absolute; left: -5000px;"><form'
and at the end
//echo '</form>';
echo '</form></div>'
In case you want to insert the whole form in an existing div, you cannot do this with PHP. As once the HTML is renedered, you cannot insert HTML using PHP.
However you can use javascript or in your case AJAX. Save the whole HTML that you are echoing, in a variable. Then pass that variable in an AJAX call.
$.ajax({url: "urFile.php"}).done(function( stringOfHTMLYouFormed ) {
$("#divID").append(stringOfHTMLYouFormed );
});
I added $parent = $input->parentNode->getAttribute('style'); into the foreach loop to look for the style of the Parent.
Right after it I use to test if the $parent has the desired style to wrap then the corresponding input field into a div.
$parent = $input->parentNode->getAttribute('style');
if ($parent == 'position: absolute; left: -5000px;') {
echo '<div style="position: absolute; left: -5000px;">';
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '></div>';
} else {
echo '<input';
if (!empty($input_name)) { echo ' name="'. $input_name .'"'; }
if (!empty($input_value)) { echo ' value="'. $input_value .'"'; }
if (!empty($input_id)) { echo ' id="'. $input_id .'"'; }
if (!empty($input_type)) { echo ' type="'. $input_type .'"'; }
if (!empty($input_class)) { echo ' class="'. $input_class .'"'; }
echo '>';
}
Related
I am currently trying to create a class schedule which I pull from my Sql Server database with PHP and I am trying to get the layout output as well as the data as I am grouping the resources.
These groupings are nested such as:
-DAY
--TIME
---CLASS
----STUDENTS
And should output like this:
However, I am getting this:
My current output is working, however, it is only on the first loop, then everything goes haywire. I am assuming there is an erroneous </div> tag somewhere in my code yet I cannot for the life of me find it.
My php code is a function that is delcare as such:
<div class="mainScheduleWrapper">
<?php daySchedule(); ?>
</div>
My php code is as such:
function daySchedule() {
global $conn;
$dayScheduleQuery = 'SET DATEFIRST 1
SELECT [DAY].[DAY] AS [DAY], CLASS.CLASSTIME AS CLASSTIME, CLASSLEVEL.CLASSLEVEL AS CLASSLEVEL, CLASS.MAXSTUDENT AS MAXSTUDENT, INSTRUCTOR.FIRSTNAME AS INSTRUCTOR, STUDENT.FIRSTNAME AS STUDENTFIRST, STUDENT.SURNAME AS STUDENTLAST, STUDENT.DOB AS STUDENTDOB
FROM STUDENT JOIN BOOKING ON STUDENT.ID = BOOKING.STUDENTID JOIN CLASS ON CLASS.ID = BOOKING.CLASSID JOIN CLASSLEVEL ON CLASS.CLASSLEVELID = CLASSLEVEL.ID JOIN [DAY] ON CLASS.CLASSDAY = [DAY].ID JOIN INSTRUCTOR ON CLASS.INSTRUCTORID = INSTRUCTOR.ID
WHERE [DAY].ID = (DATEPART(dw, GETUTCDATE() AT TIME ZONE \'AUS Eastern Standard Time\'))
ORDER BY CLASS.CLASSTIME ASC, INSTRUCTOR.FIRSTNAME ASC, CLASSLEVEL.CLASSLEVEL ASC';
// COUNTERS
$t = 0;
$i = 0;
//VARIABLES FOR DAY SCHEDULE
$classDay = NULL;
$classTime = NULL;
$classInstructor = NULL;
$closeClass = false;
$closeAll = false;
$queryConnector = $conn->query($dayScheduleQuery);
foreach ($queryConnector as $schedule) {
// CLASS DAY HEADER
if ($classDay != $schedule['DAY']) {
echo '<div class="grid-1">';
echo '<h1>' . $schedule['DAY'] . '</h1>';
echo '</div><!-- Day closed! -->';
$classDay = $schedule['DAY'];
}
// CLASS TIME HEADER
if ($classTime != $schedule['CLASSTIME']) {
if($classTime != $schedule['CLASSTIME'] && $t > 0) {
$closeAll = true;
goto closeAll;
}
echo '<div class="grid-12-noGutter scheduleContainer">'; //NON-CLOSED
echo '<h1>' . 'T = ' . $t . '</h1>';
echo '<div class="grid-middle-center col scheduleTimeTab">';
// FIX 3 DIGIT MILITARY TIME
if (strlen($schedule['CLASSTIME']) < 4) {
$classScheduleTime = '0' . $schedule['CLASSTIME'];
} else {
$classScheduleTime = $schedule['CLASSTIME'];
}
echo '<p>' . date('g:i A', strtotime($classScheduleTime)) . '</p>';
echo '</div>'; //CLOSE TIME TAB
echo '<div class="innerSchedule">'; // NON-CLOSED
$classTime = $schedule['CLASSTIME'];
$t += 100;
}
// INSTRUCTOR HEADER
if ($classInstructor != $schedule['INSTRUCTOR']) {
if ($classInstructor != $schedule['INSTRUCTOR'] && $i > 0) {
$closeClass = true;
goto closeClassWrapper;
}
echo '<div class="classWrapper">';
echo '<h1>' . 'I =' . $i . 'T = ' . $t . '</h1>';
echo '<div class="grid-3-middle classHeader">';
echo '<div class="col classHeaderCell' . classLevelColour($schedule['CLASSLEVEL']) . '">' . $schedule['CLASSLEVEL'] . '</div>';
echo '<div class="col classHeaderCell">' . $schedule['INSTRUCTOR'] . '</div>';
echo '<div class="col classHeaderCell">Max' . ' ' . $schedule['MAXSTUDENT'] . '</div>';
echo '</div>';
echo '<div class="grid-4-middle" id="studentHeaders">';
echo '<div class="col"><h6>Student Name</h6></div>';
echo '<div class="col"><h6>Student Birthday</h6></div>';
echo '<div class="col"><h6>Class Level</h6></div>';
echo '<div class="col"><h6>Attendance</h6></div>';
echo '</div>';
$classInstructor = $schedule['INSTRUCTOR'];
$i += 100;
}
echo '<div class="grid-4 studentRow">';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTFIRST'] . ' ' . $schedule['STUDENTLAST'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['STUDENTDOB'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">' . $schedule['CLASSLEVEL'] . '</span>';
echo '</div>';
echo '<div class="col">';
echo '<span class="studentCell">--</span>';
echo '</div>';
echo '</div>';
// GOTO TAGS
closeClassWrapper: {
if ($closeClass === true) {
echo '</div>';
$closeClass = false;
$i = 0;
}
}
closeAll: {
if ($closeAll === true) {
echo '</div>';
echo '</div>';
echo '</div>';
$closeAll = false;
$t = 0;
$i = 0;
}
}
}
}
Any help would be greatly appreciated - even if it's to tell me I'm going about it the completely wrong way.
Kindest Regards
Michael Z
I wouldn't say you're going about it the completely wrong way, but a few red flags jumped out at me in your code.
The use of goto jumping is bad practice. It butchers your program flow and forces you to segregate tasks that shouldn't be kept apart. You marked the sections of code "// NON CLOSED" when there was a </div> missing, is there any purpose for that? How do you know the goto sections are reliable?
When you echo something like <div class="col">, without escaping the double-quotes (as in \" for every " character), it can be problematic. Your code can get mangled or misinterpreted, both on the PHP end or on the HTML end.
Like others have said, the use of PHP may be overkill here. Besides just sending the JSON, the rest could be handled with JavaScript.
I have a function that creates a radio button in PHP:
// This function creates a radio button.
// The function takes two arguments: the value and the name.
// The function also makes the button "sticky".
function create_radio($value, $name = 'gallon_price')
{
// Start the element:
echo '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
echo ' checked="checked"';
}
// Complete the element:
echo " /> $value ";
} // End of create_radio() function.
I then leave the PHP form to create an html form and call the function three times with values that represent three different gas prices.
<span class="input">
<?php
create_radio('3.00');
create_radio('3.50');
create_radio('4.00');
?>
</span>
I am wondering how I could change this code so it would be possible to get the same output and only make one call to the create_radio function.
Thanks!
function create_radio($value, $name = 'gallon_price')
{
$output = "";
if (is_array($value)) {
while (count($value) > 0) {
$arr_value = array_pop($value);
$output .= create_radio($arr_value);
}
} else {
// Start the element:
$output .= '<input type="radio" name="' .
$name .'" value="' . $value . '"';
// Check for stickiness:
if (isset($_POST[$name]) && ($_POST[$name] == $value))
{
$output .= ' checked="checked"';
}
// Complete the element:
$output .= " /> $value ";
}
return $output;
}
A quick bit of recursion will allow the function to work for arrays and non arrays. Note: in the html you will need to echo the call to create_radio not just call it.
you could make $value an array create_radio(array('3.00','3.50','4.00')); just loop inside the function:
function create_radio($value,$name = 'gallon_price'){
foreach($value as $v){
// Start the element:
$out. = '<input type="radio" name="'.$name.'" value="'.$v.'"';
// Check for stickiness:
if(isset($_POST[$name])&&($_POST[$name]==$v)){
$out .= ' checked="checked"';
}
// Complete the element:
$out .= " /> $v ";
}
return $out;
} // End of create_radio() function.
call it:
echo create_radio(array('3.00','3.50','4.00'));
it is usually better not to echo inside the function.
I have here a script
<div>
<?php
echo '<strong>Other information</strong><br />';
$myname = get_post_meta($post->ID, 'acidity_gl', true); if ( $myname ) { echo 'Acidity: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'winePh', true); if ( $myname ) { echo 'Wine PH: ' . $myname . '<br />'; }
$myname = get_post_meta($post->ID, 'residual_sugar_gl', true); if ( $myname ) { echo 'Residual Sugar gl: ' . $myname . '<br />'; }
?>
</div>
I would like to add a condition when if $myname has a value then display the DIV but when there is no value on those 3 $myname then dont display DIV
Try this,
<?php
$myname=array();
$name = get_post_meta($post->ID, 'acidity_gl', true);
if ( !empty($name) ) {
$myname[] = 'Acidity: ' . $name ;
}
$name = get_post_meta($post->ID, 'winePh', true);
if ( !empty($name) ) {
$myname[] = 'Wine PH: ' . $name ;
}
$name = get_post_meta($post->ID, 'residual_sugar_gl', true);
if ( !empty($name) ) {
$myname[] = 'Residual Sugar gl: ' . $name;
}
if(!empty($myname))
{
echo '<div>';
echo '<strong>Other information</strong><br />';
echo implode('<br />',$myname);
echo '</div>';
}
?>
if(!empty ( $myname ))
{
// write your code for DIV here
}
Use this code:
<?php
if(!empty($my_name)){
?>
<div>DIV Content here</div>
<?php
}
?>
Or
<?php
if(!empty($my_name)){
echo '<div>DIV Content here</div>';
}
?>
I'm trying to understand when I run the btn_actions function I have nothing returned to me. I'm attempting to get two buttons to be echoed out in my table.
echo $this->functions_model->btn_actions(1);
die();
public function btn_actions($item_id)
{
$content = '<div class="controls center">';
$content = $this->btn_edit($item_id);
$content = $this->btn_delete($item_id);
$content = '</div>';
return $content;
}
public function btn_edit($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/edit/' . $item_id .'" oldtitle="Edit Task" aria-describeby="ui-tooltip-8"><span class="icon12 icomoon-icon-pencil"></span></a>';
return $button;
}
public function btn_delete($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/soft-delete/' . $item_id .'" oldtitle="Delete Task"><span class="icon12 icomoon-icon-remove"></span></a>';
return $button;
}
You need to concatenate using .
$content = '<div class="controls center">';
$content .= $this->btn_edit($item_id);
$content .= $this->btn_delete($item_id);
$content .= '</div>';
return $content;
Try this :
echo $this->functions_model->btn_actions(1);
die();
public function btn_actions($item_id)
{
$content = '<div class="controls center">';
$content = $content . $this->btn_edit($item_id);
$content = $content . $this->btn_delete($item_id);
$content = $content . '</div>';
return $content;
}
public function btn_edit($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/edit/' . $item_id .'" oldtitle="Edit Task" aria-describeby="ui-tooltip-8"><span class="icon12 icomoon-icon-pencil"></span></a>';
return $button;
}
public function btn_delete($item_id)
{
$button = '<a class="tip" href="'. current_url() . '/soft-delete/' . $item_id .'" oldtitle="Delete Task"><span class="icon12 icomoon-icon-remove"></span></a>';
return $button;
}
I have a php script that will loop until all the skill, exp and rating variables are displayed that the user entered into the database.
What I want is the following code to only display once and only if the variables are holding info and not empty. How can I do this. I hope I explained it okay.
I want this code displayed first.
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
Here is the full code.
<?php
$mysqli = new mysqli("localhost", "root", "", "sitename");
$dbc = mysqli_query($mysqli,"SELECT * FROM skills WHERE user_id='3'");
if (!$dbc) {
print mysqli_error();
}
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
echo '<div class="s">';
echo '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '</div>';
?>
you can take the following after the loop:
echo '<div id="con">';
echo '<h2 id="s">Skills</h2>';
echo '<h2 id="exp">Exp</h2>';
echo '<h2 id="r">Rating</h2>';
and instead of echo in the loops , use vars to store the text and then check if the var holds something and then echo
update:
would become something like this:
$skills = "";
$exps = "";
$ratings = "";
while ($row = mysqli_fetch_assoc($dbc)) {
if (!empty($row['skill']) || !empty($row['exp']) || !empty($row['rating'])) {
if (! empty($row['skill'])) {
$skills .='<div class="s">';
$skills .= '<p>' , htmlspecialchars($row['skill']) , '</p>';
}
if (! empty($row['exp'])) {
$exps .= '<div class="s">';
$exps .= '<p>' , htmlspecialchars($row['exp']) , '</p>';
}
if (! empty($row['rating'])) {
$ratings .= '<div class="s">';
$ratings .= '<p>' , htmlspecialchars($row['rating']) , '</p>';
}
}
}
echo '<div id="con">';
if($skills){
echo '<h2 id="s">Skills</h2>';
echo $skills;
}
if($exps){
echo '<h2 id="exp">Exp</h2>';
echo $exps;
}
if($ratings){
echo '<h2 id="r">Rating</h2>';
echo $ratings;
}
echo '</div>';