querying a hidden field in admin page - wordpress

I have 2 hidden fields called hdnBrId and hdnCmdAction.
I set them with javascript calls from their grid row cells eg hdnCmd is set to "Edit" and the row id is set similarly. and then a post from jquery
When i try query their values (before the if or before any javascript) (even with an echo) eg
echo $_POST['hdnCmd'];
I get nothing
and
if($row->BrId == $_POST["hdnBrId"] and $_POST["hdnCmd"] == "Edit")
{
echo "in edit mode";
obviously is not working.
Anybody know what to here ?
thx

Basically it seems on "submit" or "postback" because of the stateless nature of web, it will be lost, though it should still be query-able before resubmitting to the browser,...using $_POST["hdnThingy"].
Here, what happened was they were not being set correctly.
to do so correctly (in this case, I generate a html table of rows with db data and..
<input type="hidden" name="hdnThingy" id="hdnThingy" **value="<?php echo $Sel;?>"** >
I use id and name as there is some javascript and jQuery script which interrrogates these hidden fields for different reasons.

Related

custom url for custom post type search

I have custom post type Events..
URL is www.mysite.com/events/eventname, these events will have people joining them which I plan to solve by building custom DB table and placing event id and user id inside.. Now what I want and don't know even what to type in google is how to make and rule so when somebody what to see whoi s going to event that he can type URL www.mysite.com/events/eventname/users and the specific template will be pulled which will query that custom DB and show what users are attending..
I will figure out query code just need help how to make that custom url to load that query ? Is this possible within Wordpress ?
Maybe consider using php's $_GET or $_POST to do this instead. It will work fine in WordPress too.
Warning untested code ahead.
$_GET request
Sending a $_GET request using a url looks something like this:
www.mysite.com/events/?eventname=myeventname&users=true
You can then use an if statement in your php on the events page to load data depending on whether these variables are included in the url. Something like:
if(isset($_GET['eventname']) && isset($_GET['users']) && $_GET['eventname'] == 'myeventname' && $_GET['users'] == true){
//Spit out all the users here
}else{
//Continue as if nothing happened
}
$_POST Request
Post request is another way of sending data from one page to another (or sending data to itself) and if you go with this method you won't be making any changes to the url. The most common way to send a $_POST request is with a form. In this case it might be a form with some invisible inputs that will hold our data. Something like:
<form action="www.mysite.com/events/" method="post"> <!-- Submitting form to self -->
<input name="eventname" type="hidden" value="myeventname"></input>
<input name="users "type="hidden" value="true"></input>
<input type="submit" value="See who's going"></input>
</form>
And then retrieve the data when the form is submitted:
if(isset($_POST['eventname']) && isset($_POST['users']) && $_POST['eventname'] == 'myeventname' && $_POST['users'] == true){
//Spit out all the users here
}else{
//Continue as if nothing happened
}
Finally, just on the custom DB table, it might be a good idea to use WordPress users rather than building out another table. You will then be able to make use of all of those handy functions that WordPress comes with.
Best of luck!

What is the most efficient way of filling in details on a web form?

Take a standard web page with lots of text fields, drop downs etc.
What is the most efficient way in webdriver to fill out the values and then verify if the values have been entered correctly.
You only have to test that the values are entered correctly if you have some javascript validation or other magic happening at your input fields. You don't want to test that webdriver/selenium works correctly.
There are various ways, depending if you want to use webdriver or selenium. Here is a potpourri of the stuff I'm using.
Assert.assertEquals("input field must be empty", "", selenium.getValue("name=model.query"));
driver.findElement(By.name("model.query")).sendKeys("Testinput");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("Testinput", selenium.getValue("name=model.query"));
With webdriver only:
WebElement inputElement = driver.findElement(By.id("input_field_1"));
inputElement.clear();
inputElement.sendKeys("12");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("12", inputElement.getAttribute("value"));
Hopefully, the results of filling out your form are visible to the user in some manner. So you could think along these BDD-esque lines:
When I create a new movie
Then I should see my movie page
That is, your "new movie" steps would do the field entry & submit. And your "Then" would assert that the movie shows up with your entered data.
element = driver.find_element(:id, "movie_title")
element.send_keys 'The Good, the Bad, the Ugly'
# etc.
driver.find_element(:id, "submit").click
I'm just dabbling in this now, but this is what I came up with so far. It certainly seems more verbose than something like Capybara:
fill_in 'movie_title', :with => 'The Good, the Bad, the Ugly'
Hope this helps.

Post Redirect Get - how is POST never ever ever resubmitted?

There are many interesting articles on the Post Redirect Get pattern for example here: http://www.theserverside.com/news/1365146/Redirect-After-Post
But here's a simple question...
If the user does POST and is redirected to a GET. Fine if they hit refresh the browser just sends GET, easy to understand. But if the hit the BACK button after the GET and then hit refresh they can surely hit the POST again? yeah?
I am trying to understand how we can be 100% sure the POST can never be resubmitted?
One method for ensuring that a POST is not resubmitted is have a unique identifier associated with that post session, for example, if it's a shopping cart, when they begin checking out, generate a unique ID for that process. Once the checkout has completed (e.g. POST has been sent), remove that ID from the ID's that can be used.
You could also do this by generating a unique key with the form, and if the form is submitted, remove that key from where it is stored.
<input type="hidden" name="key" value="<?php echo generateUniqueKey(); ?>" />
where the generateUniqueKey() function will query a table and insert a unique ID, then return the ID. On the page where you are processing the form, do something like this:
<?php
$key = $_POST['key'];
if (isKeyStillValid ($key)) {
markKeyAsInvalid ($key);
// Process form ...
}
else {
die ("You have already submitted this form".);
}
?>
Where the isKeyStillValid() function will check the database to ensure the key used with the form is still a useable key, and the markKeyAsInvalid() function will remove the key from the database.
Update:
Here's an example that I just made which involves exactly what I described earlier. This is a very simple example, and simply uses an auto-incrementing ID in a SQL table as the key, but it should be sufficient as an example. Realistically, you would want something more thought out than this.
http://alexloney.com/post/
But if the hit the BACK button after the GET and then hit refresh they can surely hit the POST again? yeah?
Yeah.
When the user uses the back button, this can happen - the pattern doesn't protect against that, just against having the result coming up in the same page as the original form, where a refresh (F5) would cause a repost.
how we can be 100% sure the POST can never be resubmitted?
One way is to check the posted values against all values submitted in the last X minutes, discarding duplicates (at the risk of losing intentional duplicates).

Is this a KnockoutJS bug or am I doing multiple bindings wrong?

I've been working my way through the KnockoutJS documentation and tried to modify example 3 of the "Writeable computed observables" section in this page.
The example basically shows a textbox and displays a message if the user enters a non-numeric value to the textbox. I tried to modify the code so that the textbox has a pink background when the message appears.
The problem is when you enter a invalid value the textbox turns pink as expected but the value you entered is replaced with what was originally there. I have no idea why this behavior is occurring since everything worked fine before I added the style binding to get the pink background. Try removing the style binding and notice how the behavior changes when you enter an invalid value.
What's going on?
The code is below or try out this jsfiddle.
<p>
Enter a numeric value:
<input data-bind="value: attemptedValue
,style: {backgroundColor: lastInputWasValid() ?
'transparent' :
'pink' }"/>
</p>
<div data-bind="visible: !lastInputWasValid()">That's not a number!</div>
function MyViewModel() {
this.acceptedNumericValue = ko.observable(123);
this.lastInputWasValid = ko.observable(true);
this.attemptedValue = ko.computed({
read: this.acceptedNumericValue,
write: function (value) {
if (isNaN(value))
this.lastInputWasValid(false);
else {
this.lastInputWasValid(true);
this.acceptedNumericValue(value); // Write to underlying storage
}
},
owner: this
});
}
ko.applyBindings(new MyViewModel());
EDIT: Here's another fiddle with the style binding removed. Try appending the letter 'a' and taking focus out of the textbox. Notice how the letter 'a' stays there. Try that with the original fiddle textbox and notice how it is removed. The only change between the two fiddles is the presence of the style binding.
If the value is NAN than it is never written to the model, therefore the input will be updated to the existing value of the model when the onblur event is fired.
this.acceptedNumericValue(value); // Write to underlying storage
Is the code that updates when the value is numerical. You can see that it is not in the else block.
So I sent an email to the KnockoutJS user group and got a reply in about 7 hours (not too shabby).
Sadly, Google Groups confuses me and I have no idea how to reply to the fellow who cleared up my question to tell him to come on over here and post his answer so I guess I'll do it for him. All credit goes to John Earles of the KO user group.
It make sense to me.
In your example without the style, Knockout does not have to re-render
your input (only the error), so the value stays the same.
In your example with the style, Knockout does have to re-render your
input (to add the style), so BOTH bindings execute and it reads the
value - which is the last accepted value.
Here is a version that saves the attempted value into one of two
observables, and reads from the appropriate one based on
lastInputWasValid:
http://jsfiddle.net/jearles/VSWfr/

Read from values from hidden field values in Jquery?

Last two nights I am struggle with below code. The problem is I need to remember expanded (or) collapsed toggles sections and on page reload I have show them as is expanded (or) collapsed.
$(function() {
$('tr.subCategory')
.css("cursor", "pointer")
.attr("title", "Click to expand/collapse")
.click(function() {
$(this).siblings('.RegText').toggle();
});
$('tr[#class^=RegText]').hide().children('td');
})
I found small solution in another forum like this. Storing ".subCategory" id values in hidden field in commas-seperated values.
In Asp.net page:
<input id="myVisibleRows" type="hidden" value="<% response.write(myVisibleRowsSavedValue) %" />
In .js:
var idsStr = $(#myVisibleRows).val();
Now My question is: How to store multiple values (.subCategory id) in hidden field when I click on toggle?. also How to parse them back and iterate them get ids and show toggles?. I am very very new to jQuery. Please some one help me out from this.
Passing this kind of values in a form isn't probably the best thing to do it. I'd suggest using cookies to store expanded sections id's or something similar. It's much easier to implement and you are not passing unimportant form data between requests. If you want to store multple values you can serialize them (easiest thing: use join function in JS) before you store them and deserialize (split) after reading it from a cookie.

Resources