I want to create a form with two fields. The first field lists all pages and the second field lists all child pages (if they exist) of the chosen page from the first field. When the form is submitted the site redirects the user to the chosen page. How can I implement this form?
You're going to need to do an AJAX request to get the second drop down list.
There is a function built into WordPress which gets the pages and you can specify parent ID's and also to show only top level pages.
Heres an example of the code but you'll need to then implement it in a way that best suits your requirements
<?php
$top_level_pages = get_pages(array('parent'=> 0));
?>
<select>
<?php
foreach($top_level_pages as $top_level_page) {
echo '<option>'.$top_level_page['post_title'].'</option>';
}
?>
</select>
You will then need to determine what option the user has clicked and run an AJAX request to fill the second select.
The second select should look something like this:
<?php
$id = $_GET['page_id']; // get the id of the page from the first select
$child_pages = get_pages(array('child_of'=>$id));
?>
<select>
<?php
foreach($child_pages as $child_page) {
echo '<option>'.$child_page['post_title'].'</option>';
}
?>
</select>
Read more about the get_pages() function here: http://codex.wordpress.org/Function_Reference/get_pages
Related
I'm new to Yii framework and I'm designing a form (I'm using create form) to create a row in database.
Let me explain about the scenario succintly, so that I can make it clear for what I want-
I have 10 fields in this form. Out of this 10 fields, five fields change dynamically.
I created two div's basically div A and div B and repeated the fields as required for the two cases.
Say some fields which are textfields in div A will be dropdownlists in div B.
<div id="A">
<?php echo $form->labelEx($model,'selectionList'); ?>
<?php echo $form->textArea($model,'selectionList',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'selectionList'); ?>
</div>
<div id="B">
<?php echo $form->labelEx($model,'selectionList'); ?>
<?php echo $form->dropdownList($model,'selectionList',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'selectionList'); ?>
</div>
I have two radiobuttons say Single and Multi. When I select Single radiobutton, div A should be considered and div B discarded and when I select Multi, vice versa should happen.
So, is this the right way to change the form fields dynamically. Or else how can I do this using Ajax validation.
Not so related to question:
1) You are using same name for all fields, so only last one will be submitted (check generated html, name is MyModel[selectionList] and not MyModel[][selectionList]
2) You will have lots of problems with Ajax validation and dynamic fields (generated via js), since CActiveForm will generate js code via php.
Related to question:
3) To use one or another field, i suggest you to have two separate field names and just hide with js/css one, that is not needed right now. After submitting, just check value of radio button and decide what fields will be saved.
To validate these inputs, you have to use model scenarios (will define in rules what to validate and what not to):
$model = MyModel();
if ($_POST['C'] == 'multi') {
$model->scenario = 'validateOnlyFirstField';
} else {
$model->scenario = 'validateOnlySecondField';
}
Also you can use second CActiveForm.validate() parameter to define what attributes to validate.
I am trying to write a wordpress plugin and I have hit a bump. I am new to PHP (coded in Java before) and javascript so I am not sure whats the best way to solve my problem.
The Background
I have some data in a mySQL DB that I am using (each row has a unique ID and some other information I have added). I am able to search the DB using
$headss = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}costumesdb WHERE location = 'head'", ARRAY_A);
And display some of the information to the user using (this is one of 5 different drop-downs but they are all created in the same way)
Head: <select name="head">
<?php foreach ($heads as $head) { ?>
<option value="<?php echo $head['pieceName'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
<?php } ?>
</select>
For the moment I want the user to be restricted to choosing information that is already in the system.
The problem
The DB contains 2 pieces of information that the user does not need to know to fill in the form (a website URL and a picture URL). I need these 2 pieces of information once the form is submitted (I need to write some more code for that) to the server which spits out another page with the 2 URL's in it.
Whats the best way to send the data back to a PHP script? Am I able to access the row of data that the user has selected in the drop down and send the unique ID for that row back or do I need to do something else?
Edit:
This is the script that I am using to submit the code:
$('#createacostume').form({
success:function(data){
$.messager.alert('Info', data, 'info');
}
});
'
And then the page to display the information returned is:
$cname = $_POST['cname'];
$head = $_POST['head'];
echo "Data Returned Name $cname head $head
I think this is what you are asking:
User has to choose an item from a drop down and submit a form. You have to display the website URL and the image for that item in a second page. You want to know how this is typically accomplished.
If that's the case, you should pass the row id of the item to the second page like so:
<option value="<?php echo $head['ROW_ID'] ?>"><?php echo $head['shopName'] . " - " . $head['pieceName'] ?></option>
Then use the ROW_ID in the second page to access the data from the database and print out the website URL and the image.
Submit the first form (without the two field), INSERT the data into the database, get the ID of insert.
Pass the ID to the next page which would set the ID into a hidden form field (or GET or POST parameter, plenty of choices) of the new form (with the two fields and just UPDATE the database upon submitting the second form.
If you like to show the original data in the second form, just pull the data from the database and use it to render the form instead of passing just the ID into a hidden field.
I have a PHP page which displays the results of a users database query for a classified ad's site. The user initiates a search on the homepage and selects the parameters of the search using select boxes and then the form is submitted.
Once on the results page I need to allow the user to modify the search parameters using select boxes down the side of the page (the same ones on the homepage). How do I go about making the option selected in each select box match what was selected on the first page. i.e. if the user selects make: Audi and Model: A8 I want the drop down for make and model to list all the other options, but have the Make preselected to Audi and the Model pre-selected to A8.
I know about using option selected for option boxes in a select statement, but how can I take a select statement that has already been coded for the results page and insert a selected variable for the option that should be selected first.
I hope that makes sense. I haven't posted any code because I'm in search of ideas rather than help fixing a code problem.
When the user submits the search form, all the variables posted in the form is available to you via $_POST or $_GET. Use these variables to re-select the correct information in the search form on the results page.
<select name="model">
<option value="a8" <?php echo (isset($_POST['model']) && $_POST['model'] == 'a8') ? 'selected' : ''; ?>>A8</option>
</select>
EDIT
The dynamic database way:
<select name="model">
<?php
$getRows = mysql_query("SELECT * FROM models");
while($row = mysql_fetch_assoc($getRows))
{
echo '<option value="" ' . ((isset($_GET['model']) && $_GET['model'] == $row['model']) ? 'selected' : '') . '></option>';
}
?>
</select>
Now you only need one if statement. You should btw use GET parameters, so that the user has the opportunity to bookmark the results for later use.
I have two <select> one is category and the second is subcategory.
here is the first <select> for category.
<select name="category" size="10">
<?php foreach($categories->fetch(array('table' => 'categories')) as $category) { ?>
<option value="<?php echo $category['id']; ?>"><?php echo $category['name']; ?></option>
<?php } ?>
</select>
now the second <select> i.e subcategory should be hidden initially and when a user click on category <select> based on the value it should populate the value in subcategory.
One way of doing this is via AJAX by passing categoryId as POST Request and getting HTML as response.
however i would like to know if there is any other alternative so that it automatically pass the categoryId value to PHP and unhide the second <select> here is the code of second <select>
<select name="subcategory" size="10">
<?php foreach($categories->fetch(array('table' => 'subCategories', 'categoryId' => $categoryId)) as $subCategory) { ?>
<option value="1"><?php echo $subCategory['name']; ?></option>
<?php } ?>
</select>
the only thing i need here is $categoryId to be populated dynamically. is there any way of doing this?
thank you..
No, there is no way to do what you are suggesting. PHP is only run on the server, so by the time the page is rendered on the client the PHP has already been run.
Your best bet would be what you already suggested, running some AJAX after the first select is changed, sending back the category ID to the server and retrieving what you need to build the second select.
Is there a reason why you don't want to do it this way?
Sukumar has probably suggested the best and most intuitive solution to make it appear as if the data is being loaded dynamically to the user.
The other alternative would be to submit the form when the select box is changed. Once the form has been submitted PHP would pick up the ID from the POST array and then re-populate the sub-category select box. This is often used as a fallback in case the user doesn't have JavaScript enabled.
Structurally, there are three choices to solve this problem:
Use an ajax call to fetch the required data when a user selection is made as jbruno has described.
Submit the whole page to the server, let your PHP see the newly selected option and fill in the newly desired data in a returned page. This will cause the page to refresh so is less ideal than option 1.
Pre-populate the page with all possible data in a javascript data structure so you can use Javascript to just look up the desired category ID in a local data structure, modify the page and never have to talk to the server in order to update the page.
In my opinion, option 3) is the most desirable if the data set required for local lookup is not too large (say under 100k) and it's not too expensive on the server to collect all that data for inclusion in the original page and if the data doesn't change real-time or having data as of the page load time is OK.
If option 3) isn't feasible for any reason, then option 1) is next best. Option 2) is not as good a user experience so it should only be the last resort if you really can't implement options 1) or 3).
You asked more specifically about option 3. I don't really yet understand what the whole data you need looks like. If you really only have four total data types residential_plot, residential_apartment, office_space and showroom, then you can just make those be four keys on an object and store their data that way:
var data = {
"residential_plot": 1,
"residential_apartment": 2,
"office_space": 3,
"showroom": 4
};
The 1, 2, 3 and 4 are just whatever data you want to store for that type. It can be numbers, strings, arrays of data, other objects of data, anything.
To access this, you would do like this:
var id = data.residential_plot;
or
var index = "residential_plot";
var id = data[index];
If you wanted to store the notion of categories and sub-categories, you would need an extra level of objects:
var data = {
"residential": {"residential_plot": 1, "residential_apartment": 2},
"commercial": {"office_space": 3, "showroom": 4}
};
Then, you would access it like this:
var id = data.residential.residential_plot;
or like this:
var category = "residential";
var catType = "residential_plot";
var id = data[category][catType];
I have this webpage. It has a page called "services.php". I have several buttons (made of classes), that belong to different "package" prices i offer.
I want the links that say "Select" to autofill a form in another page, or alternativly in a popup form in the page..
I don't really know how to explain it, but as short as possible:
When link is pressed autofill form (in this or other page) with the type of package they chose. Only text autofill
What you seem to be asking is 'loading' a page pre-filled with specific information, you can do this a number of ways, either by utilizing javascript (like jQuery for instance). Or using your PHP, make links that pass variables (say a flag or a reference to pre-fill the fields -- if you want a popup or next page, etc).
Your url would like like the following for the button that a user presses (button would be a simple http link):
http://mywebsite.com/prefill.php?user=bob&package=2
This would have the values bob as the user that requests it (you can reference an id for user info here as well), and package=2 to designate your package options.
Then on the prefill.php page, you would have something that checks for:
$user = $_GET['user'];
$package = $_GET['package'];
Hope that helps
This will populate form fields with whatever you pass to the autoFill() function. This would be a same page example.
<html>
<body>
<form>
<input type="text" id="packageDescription">
<input type="text" id="packagePrice">
</form>
<script>
function autoFill(packageDescription, packagePrice) {
document.getElementById('packageDescription').value = packageDescription;
document.getElementById('packagePrice').value = packagePrice;
}
</script>
Premium Package<br>
Platinum Package
</body>
</html>
You could do something like this:
<select id="packages">
<option value="package1">Package 1</option>
<option value="package2">Package 2</option>
</select>
Submit
When the link is clicked, the following javascript will fire off:
function submitPackage()
{
var package = $("#package").val();
window.open("http://your-site.com/some-script.php?package=" + package);
}
The above will open a pop up window to a page such as this:
http://your-site.com/some-script.php?package=package1
In some-script.php you will do something like this:
You selected the package: <b><?php echo $_GET['package'];?></b>.
Or:
<?php
//Put the packages in an array:
$packages = array();
$packages['package1'] = 'Package 1';
$packages['package2'] = 'Package 2';
//...
?>
<select id="package">
<?php foreach ($packages as $name => $text):?>
<? $selected = ($name == $_GET['package']) ? 'selected' : '';?>
<option value="<? php echo $name;?>" <?php echo $selected;?>>
<?php echo $text;?>
</option>
<? endforeach;?>
</select>
The above will auto select the package they selected in a dropdown box.
if i understood your problem, you want to fill some input fields with information when the user clicks on some links
i can think of 2 ways of doing this : either have the links point to a page like services.php?req=package1 (or any other url you want) and on that page generate the input fields with the information you need (set the default values in the fields with the ones you want), or, use javascript to change the values of the forms without changing the actual page (either via ajax or predefined values)
for javascript you can use the jQuery framework, it has a pretty extensive community of enthusiasts and plenty of examples to get you started with it.
an example for your case would be
$('#btn1').bind('click', function() {
$('#input1').val("value");
$('#input2').val("value2");
});
replace btn1 with the id of the first button or link you have, input1 with the id of the first input in your form, and value with the value you want
I just did this myself. My solution was with jQuery. Just assign an id to your link. The first ID in the code is the link id and the second is the id for the input element you want to populate.
Here is the script:
<script>
$(document).ready(function() {
$('#link_id').click(function() {
$('#input_id').val( $(this).text() ).keyup();
return false;
});
});
</script>
Hope it works!
I've ran several time into the same issue, so I had to write my own script doing this. It's called Autofiller and its pretty simple but does great job.
Here is an example
http://example.com/?autofiller=1&af=1&pof=package&package=package1
So basically it takes several parameters to init the script:
autofiller=1 - init AutoFiller
af=1 - Autofill after page is loaded
pof=package - Find the parent form element of the select with name attribute package. Works also with input form elements.
package=package1 - Will set the select element's value to package1
Hope it helps you! :)