How can I get the value of the amp selector and save it in a php variable
<amp-selector multiple layout="container" class="radio-selector"
on="select: AMP.setState({
selectedOption: event.targetOption,
allSelectedOptions: event.selectedOptions
})">
<div option="1">1</div>
<div option="1">1</div>
<div option="1">1</div>
</amp-selector>
<input type="text" name="" [value]="allSelectedOptions.join(' ')">
<?php
$category_id ='"selectedOption"';
// $category_id ='{{selectedOption}}';
// $category_id=remove_special_characters($category_id);
$int = (int) filter_var($category_id, FILTER_SANITIZE_NUMBER_INT);
echo $int;
$filter= get_filter_category($category_id);
?>
I tried multiple ways like domdocument but nothing seems to work.
Any help would be appreciated
$dom = new DOMDocument();
$dom->loadHTML($html);
//$num = "";
$optionTags = $dom->getElementsByTagName('div');
print_r($optionTags);
for ($i = 0; $i < $optionTags->length; $i++ ) {
if ($optionTags->item($i)->hasAttribute('selected')
) {
$num = $optionTags->item($i)->nodeValue;
}
}
echo "Num is " . $num;
Normally you would need this for a form.
Hence you will have a submit button or something similar.
Way to go here is to link your submit button to another file that reads the selection (possible via POST) and redirects you to the desired page.
If you just want to save your selection without doing anything, you could use amp-bind and a JSON Endpoint. The Endpoint gets called when changed, with the selection as parameter and saves it instead of delivering any files what so ever.
Related
I am trying to access different elements in an XML so I can edit them or add new ones from within a web page. I'm using PHP to loop through the parent tags and display in a select list then have submit button to POST the element selected - no issue.
I then use PHP to find all the names of the selected elements children, and display those names in another select list form. This displays the children in the list correctly however when I hit submit it displays correctly if I echo $_POST("Section") but clears the previous $_POST("Page").
I think this has something to do with using action="", but interestingly if I change one them to GET it works as intended. I actually want a 3rd step into lower children again, so cannot use that solution as a dodgy workaround.
I wont post XML as it has sensitive data in it and you can just trust I am stepping through that fine, it's the php clearing $_POST that is the big issue for me.
Please go easy - I literally learnt how to make a basic html webpage in april.
Tried a bunch of different form action including the .php it is on, and also
<?php echo $_SERVER['PHP_SELF']; ?>" >
As stated before - using one as GET and one POST seems to solve the issue
<?php
//pages processing and put into an array
$pages_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
array_push($pages_array, $page->name);
}
$pagecount = count($pages_array);
?>
<form name="page_select" method="POST" action="">
<select name="Page">
<?php
for ($i = 0; $i < $pagecount; $i++){
print '<option>'.$pages_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_page" value="edit"/>
</form>
<br></br>
<?php
// save page selected into $pg and display page being edited
$pg = $_POST["Page"];
echo 'YOU ARE EDITTING PAGE: '.$pg;
?>
****** THEN FURTHER DOWN THE PAGE *****
<?php
// section processing
$section_array = array();
$xml = simplexml_load_file('XML/links.xml') or die ("Failed to load");
foreach($xml->page as $page){
if($page->name == $pg){
foreach($page->sections->section as $section){
array_push($section_array, $section->name);
}
}
}
$sectioncount = count($section_array);
?>
<form name="section_select" method="POST" action="">
<select name="Section">
<?php
for ($i = 0; $i < $sectioncount; $i++){
print '<option>'.$section_array[$i].'</option><br>';
}
?>
</select>
<input type="Submit" name="edit_section" value="edit"/>
</form>
<br></br>
<?php
$sct = $_POST["Section"];
echo 'YOU ARE EDITTING SECTION: '.$sct;
?>
I want output to remember both arrays after the second form is submitted, however due to the first POST variable being wiped it means I also lose the second array since when it gets back to pushing to $section_array $page->name can never = $pg since $pg is now empty
Every time you submit a form, the page is reloaded and PHP will only have access to the POST variables from the form that triggered the page reload. This means that as your second form is submitted, $_POST['Page'] ceases to exist.
There are many ways around this problem, here is a fairly simple one. In your second form, create a hidden input that contains the value of the $_POST['Page'] variable. That way, when the second form is submitted, the Page value will be propagated to the next step of the process. Adding this code to your section_select form should suffice:
<?php
if(isset($_POST['Page'])) {
?>
Page: <?php echo $_POST['Page'] ?>
<input type="hidden" name="Page" value="<?php $_POST['Page'] ?>" />
<?php
}
?>
On my php buffering page, I am able to create and define a variable by using the contents of my form field, like this...
PHP:
<?php $data = $_POST['data']; ?>
And in my form, I am able to create the 'value' of the 'data' field by using php 'include' to call in the data from 'data_page.php' like this...
FORM:
<input name="data" type="text" value="<?php include "data_page.php";?>">
This process does work, however... I would like to bypass the form part of the process and still create that same variable.
I have tried a few ways to do this, including this...
<?php $data = $_REQUEST['data_page.php']; ?>
But so far, nothing seems to work.
Is there a way to create that variable with the same value (contained in data_page.php) which the form process would provide? And if so... what am I doing wrong?
If I right understand what you need then you should turn on output buffering
ob_start();
include('data_page.php');
$data = ob_get_contents();
ob_end_clean();
echo $data;
or use file_get_contents()
for ($i = 1; $i <= 5; $i++) {
${"data{$i}"} = (is_readable("data_page{$i}.php")) ? file_get_contents("data_page{$i}.php") : '';
}
I'm trying to cycle through my photo gallery with next / previous buttons. When the user clicks on a photograph it enlarges it. When they click next it should go to the next photo. When the user clicks on the submit button immediately after selecting the photo it will enlarge the next photo. However when they click submit (next) again it doesn't update the $index variable. I know the reason is because once its set the first time the isset($_POST['myNext') evaluates to false. So how can I update $index each time they click on the button. I don't want to use javascript for this because it should be possible with PHP. I also don't want to just copy and paste some JS i don't understand. I've looked online for answers but have found nothing without JS. I've omitted the code to scan the directory with the images. Thank you.
<?php
for($i = 0; $i < count($thumbArray); $i++)
{
$currentIndex = $thumbArray[$i];
echo '<div class = "img"> <img src="'.$dir.$thumbArray[$i].'" alt="Picture" height = "100" width = "100" /> </div>';
}
if(isset($_GET['original_selection']))
{
$original_selection = $_GET['original_selection'];
}
for($i = 0; $i < count($thumbArray); $i++)
{
if($thumbArray[$i] == $original_selection)
$index = $i;
}
?>
<html>
<head><title>Single</title></head>
<body>
<?php
if(isset($_POST['myNext']))
{
echo 'index: '.$index;
$index++;
}
?>
<div class = "img"> <img src="<?= $dir.$thumbArray[$index] ?>" alt = "Picture" height = "500" width = "500" /></div>
<form name="NextForm" method="post">
<br />
<input type="submit" name="myNext" value="Next"/>
</form>
<h1>hi</h1>
</body>
</html>
Have you tried to output what $index is before the if(isset($_POST['myNext']))?
You miss the action parameter in the form tag:
<form name="NextForm" method="post" action="">
^ Doing that will send the form data to the same page.
This seems like very inefficient code I'm allowed to be brutally honest.
for($i = 0; $i < count($thumbArray); $i++)
{
if($thumbArray[$i] == $original_selection)
$index = $i;
}
Why are you searching like that? Not only will it continue to search after it finds it's value, but there are already a php function for this, namely array_search. Also you don't need to loop like that in php, just use the foreach loop. If you really had to do what you tried above, you should have used "break;" inside the if.
Instead of this:
echo 'index: '.$index;
$index++;
You could have just done this:
echo 'index: '.$index++;
EDIT:
Where do you store the thumbarray? This could all have been made much easier and more efficient if you used a database to store the name of the files and gave them for example a group id number, an id etc.
I'm trying to create a repeatable field (an upload image input with image preview) in PHP using jQuery.clone(). Everything works fine, except the return of the clone data.
In my PHP file, I have:
$i = 0;
$valid_input['image'] = $input['image'][$i];
then
return $valid_input;
used in an image upload input:
<input type="hidden" class="image" name="image[image]['.$i.']" value="'.$theme_options['image'].'" />
<input type="button" class="upload-button button" value="'. __( 'Upload Image', 'theme' ).'" />
the value of $i is set to 0 and counted with jQuery clone.
The problem is that the cloned fields disappear after submitting.
The original field "image['image'][0]" is saved and returns as valid, but the others (image['image'][1], [2], [3]...) don't validate!
If I change the value of $i like this:
$i = 1;
$valid_input['image'] = $input['image'][$i];
then the original input don't submit, only the clone, because the original is [0] and the clone is [1], but after submitting the clone returns as [0].
I tried things like:
$i = 0;
$valid_input['image'] = $input['image'][$i];
$i++; //-- I know, I'm stupid...this will not count the input!
Please, can somebody help me with this?
How can I be able to validate the cloned fields?
My Google searches are all marked as visited and I swear that I did not find anything that could solve this!
Any help would be greatly appreciated, thanks in advance!
Do you mean something like:
for($i = 0; $i < count( $input["image"] ); $i++) {
echo $input["image"][$i];
}
or did i get you wrong
please try with this
using the input name as image[] so you will get the value of image as an simple array by using the foreach you can do you validation.
i.e.,
$arrValue = $_POST['image'];
foreach ($arrValue as $key=>$value) {
//do the operations here
}
Ok, this situation is a little weird but anyway. This PHP code generates several radiobuttons:
for($i = 0; $i<count($questionList); $i++)
{
echo $questionList[$i]->__get(QuestionId).'-'.$questionList[$i]->__get(QuestionText).'<br />';
$answerList = $questionList[$i]->GetAnswers();
for($j = 0; $j<count($answerList); $j++)
{
echo '<br /><input type=\'radio\' name=\'group'.$i.'\' id=\'radioButtonAnswer'.$answerList[$j]->__get(AnswerId).'\' value=\''.$answerList[$j]->__get(AnswerId).'\' >'.
$answerList[$j]->__get(AnswerText).'</input>';
}
echo '<br /><br />';
}
Ok, that works fine, after the checkboxes are created, I'm trying to run some code to get all the radio buttons and it didn't work, so I tried just getting one radio button several times, and it only gets it the first time.
function Validate()
{
var i = 1;
do
{
document.writeln(document.getElementById('radioButtonAnswer2') == null);
i ++;
}while(i < 10);
document.writeln('out of loop');
return false;
}
So I know FOR SURE that 'radioButtonAnswer2' exists and it shouldn't be null. But this is what I get when I click the submit button:
false true true true true true true true true out of loop
The first time is not null, but after that, it is. Any thoughts?
Thanks!
You can use document.getElementsByName("group") to get all the radio buttons.
This loop works fine. The problems is with document.writeln(), it replaces the html in the page and therefore the DOM elements are gone. Here's an updated version using an Alert instead.
function Validate(){
var radioGroup = document.getElementsByName("group");
var results = "";
for(i = 0, len = radioGroup.length; i < len; i++){
currentRadio = radioGroup[i];
results += "\n" + currentRadio.id + " is ";
results += (currentRadio.checked ? "checked" : "not checked");
}
results += "\n..out of loop...";
alert(results);
return false;
}
It may be because your HTML you are generating is invalid. You also shouldn't be explicitly calling the __get() function, but that's an unrelated issue most likely.
Something like:
<input type="radio" ...>Label Text</input>
is not the correct way to define a radio button.
Try this code:
for($j = 0; $j<count($answerList); $j++)
{
echo '<br /><input type="radio" name="group'.$i.'" id="radioButtonAnswer'.$answerList[$j]->AnswerId.'" value="'.$answerList[$j]->AnswerId.'" />';
echo '<label for="radioButtonAnswer'.$answerList[$j]->AnswerId.'">'.$answerList[$j]->AnswerText.'</label>';
}
Edited to add: Ah, now I see. You're using document.writeln(). That function overwrites the content of the page.
So the first time into the loop, the element does exist, and it does a document.writeln() call, which writes "true" to the page. This overwrites everything that was on the page before (didn't you notice how when the page loads, it only has the output of the javascript?). The next time through the loop, it tries to look for the radio button again, but it's been erased and replaced with the javascript output. Now it no longer exists.