Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

02-29-2012 , 03:00 AM
Is this the place for random questions?

If so, i have a PHP/MySQL set up for entering time entries for staff, basically its a form with start date/time end date/time and some information. When they click submit i need to run a query that checks if the time entry overlaps with an existing one and put an "are you sure? yes/no" message box. Here's what I have so far:

Main Form:
Code:
<input name="submit" onclick="return checkOverlappingTimeEntries(document.timeform);" type="submit" value="Submit">
Javascript:
Code:
function checkOverlappingTimeEntries(theform)
{
	XMLpostdata(theform, 'checkOverlapTimeEntries.php');
	return false; // i know this is probably wrong but if i remove this line the confirmation box never shows up and the form just submits
}
checkOverlapTimeEntries.php:
Code:
.
. a lot of unimportant code that works fine
.
if ($overlap) { 
	echo "confirm('There is already another time entry entered in this range, do you still want to save?');";
}
So basically i don't know how to pass the result of the confirmation box back to the original input "onClick". Am i going about this completely wrong or just missing something simple?

thanks!
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:22 AM
your XMLPost ajax function needs to return JSON for true/false. Then your javascript can work with the JSON. That is, the confirm code should be static code in your javascript, that uses the response returned by the server. You shouldn't have the php writing the javascript confirm code dynamically like that.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:24 AM
1. Use jquery
2. What is xmlpostdata? This afaik is not a native js function.
Anyway, this function should be returning the result it gets back from the server, and you should handle that in checkoverlappingtimeentries
3. Return false is fine, as this stops the submit click from doing its default action (submitting the form).
4. Don't pass down JavaScript code from your php server for the client to execute. Pass the state back and let a branch in the js code decide what to do. (In this case I'd probably Jude use http status codes: 200 OK v. 400 Bad Input)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:26 AM
I see, that makes sense.

so

Code:
function checkOverlappingTimeEntries(theform)
{
	var overlap = XMLpostdata(theform, 'checkOverlapTimeEntries.php');
	if (overlap) {
           return confirm("");
       }
}
i'm not sure how to return a value from xmlpostdata, kinda new to this whole javascript/php integration

edit:

Code:

function XMLpostdata (theform, helper)
{
	var urldoc = helper;

	if (xmlhttp && xmlhttp.readyState != 0)
	{
		xmlhttp.abort ();
	}
	xmlhttp = getXMLHTTP ();
	if (xmlhttp)
	{
		xmlhttp.open ("POST", urldoc, true);
		xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4 && xmlhttp.responseText)
				{
					//alert(xmlhttp.responseText);
					eval(xmlhttp.responseText);
				}
			}

		xmlhttp.setRequestHeader ("Man", "POST /" + urldoc + " HTTP/1.0");
		xmlhttp.setRequestHeader ("Accept", "text/plain");
		xmlhttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");

		var data = "";
		var i;
		for (i = 0; i < theform.elements.length; i++)
		{
			if (i > 0)
			{
				data += "&";
			}
			data += theform.elements[i].name + "=" + escape (theform.elements[i].value);
		}

		xmlhttp.send (data);

	}

	return (false);
}
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:29 AM
thanks for the quick responses btw
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 12:49 PM
Just asked this Q on SO:
http://stackoverflow.com/questions/9...e-speed-report

Anyone got any ideas? Every trick I'm trying isn't working
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 01:17 PM
Quote:
Originally Posted by Gullanian
Just asked this Q on SO:
http://stackoverflow.com/questions/9...e-speed-report

Anyone got any ideas? Every trick I'm trying isn't working
13K reputation! Holy **** my penis envy is overwhelming me! I don't think I'm fit to post in the same LC thread as you, Tom....

Spoiler:
sorry, no ideas for you actual question. i bet it's possible they've plugged all the holes really well so there's no workaround, other than the URL hack suggested by that SO poster
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 01:26 PM
On another note, can a Ruby programmer tell me why only 2 of my tests are getting run here:

http://ideone.com/04Mgc
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 01:37 PM
Coursera exercise detected! Gonna work on that today/tomorrow myself

Edit: seems test 2 and 3 have the same name
Code:
def test_num_of_players_errors
line 35+53
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 01:42 PM
doh! tyvm clowntable
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 01:54 PM
Mmmm I think I'll use cucumber for the palindrome stuff just for kicks :P
Will post spoilered when done, maybe as a step by step guide
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:16 PM
please do.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:24 PM
Quote:
Originally Posted by gaming_mouse
13K reputation! Holy **** my penis envy is overwhelming me! I don't think I'm fit to post in the same LC thread as you, Tom....
Lol unfortunately I've asked a lot of questions so it's not as amazing as it looks, I shamelessly abused it as my personal tool to learn C#.

In my old job ages ago I was tasked with building a website in C# and I only knew Classic ASP so it was a challenge, to say the least, my work wouldn't send me on courses and noone else around me knew anything about it so SO was my bastion of hope as my primary help tool when I got stuck (and I got stuck a lot). Anyway learning via SO worked I think! I've had a couple of good answers as well
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:30 PM
am I being petty/cheap by being slightly annoyed by the SAAS free course asking for 10 bucks for their ebook?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:40 PM
Quote:
Originally Posted by Gullanian
Lol unfortunately I've asked a lot of questions so it's not as amazing as it looks, I shamelessly abused it as my personal tool to learn C#.

In my old job ages ago I was tasked with building a website in C# and I only knew Classic ASP so it was a challenge, to say the least, my work wouldn't send me on courses and noone else around me knew anything about it so SO was my bastion of hope as my primary help tool when I got stuck (and I got stuck a lot). Anyway learning via SO worked I think! I've had a couple of good answers as well
In my experience, writing good questions can get you more rep than good answers.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:43 PM
Quote:
Originally Posted by Gullanian
Just asked this Q on SO:
http://stackoverflow.com/questions/9...e-speed-report

Anyone got any ideas? Every trick I'm trying isn't working
What about passing an argument in the URL, e.g. www.scirra.com?noSocial=1 and not rendering the social network snippets on that basis?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:45 PM
Yeah that's what we did in the end! Seems to be the only way. It's still a 'problem' I'd like to solve though as I strongly suspect google webmaster tool count these times in their total page speed which can be damaging to ranking.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 02:54 PM
Quote:
Originally Posted by e i pi
am I being petty/cheap by being slightly annoyed by the SAAS free course asking for 10 bucks for their ebook?
i think you won't be annoyed once you see the quality of the book and screencasts and overall thought that went into this curriculum.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:13 PM
0) Coding on Windows is a pain lol
0b) Install cucumber: gem install cucumber (+make available to path should be automatic on unixoid systems)
---

1) Create a features directory
2) Inside create a file called palindrome.feature

Code:
Feature: Detect Palindromes

As a user I want to be able to check if a given word or phrase is a palindrome.

Scenario Outline: Provide input to palindrome?
  Given the input "<input>"
  When palindrome? is run
  Then the output should be "<output>"
  
Examples:
  |input|output|
  |A man, a plan, a canal -- Panama|true|
  |Madam, I'm Adam!|true|
  |Abracadabra|false|
3) Run "cucumber" from the parent directory of the features directory (i.e. homework1) you'll get a nice and helpfull warning
Code:
Feature: Detect Palindromes

  As a user I want to be able to check if a given word or phrase is a palindrome.

  Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
    Given the input "<input>"                    # features\palindrome.feature:6
    When palindrome? is run                      # features\palindrome.feature:7
    Then the output should be "<output>"         # features\palindrome.feature:8

    Examples:
      | input                            | output |
      | A man, a plan, a canal -- Panama | true   |
      | Madam, I'm Adam!                 | true   |
      | Abracadabra                      | false  |

3 scenarios (3 undefined)
9 steps (9 undefined)
0m0.021s

You can implement step definitions for undefined steps with these snippets:

Given /^the input "([^"]*)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

When /^palindrome\? is run$/ do
  pending # express the regexp above with the code you wish you had
end

Then /^the output should be "([^"]*)"$/ do |arg1|
  pending # express the regexp above with the code you wish you had
end

If you want snippets in a different programming language,
just make sure a file with the appropriate file extension
exists where cucumber looks for step definitions.
4) All right..create the step_definitions directory and a file palindrome_steps.rb in it with the C+Ped code and run "cucumber" again
Code:
Feature: Detect Palindromes

  As a user I want to be able to check if a given word or phrase is a palindrome.

  Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
    Given the input "<input>"                    # features/step_definitions/palindrome_steps.rb:1
    When palindrome? is run                      # features/step_definitions/palindrome_steps.rb:5
    Then the output should be "<output>"         # features/step_definitions/palindrome_steps.rb:9

    Examples:
      | input                            | output |
      | A man, a plan, a canal -- Panama | true   |
      TODO (Cucumber::Pending)
      ./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
      features\palindrome.feature:6:in `Given the input "<input>"'
      | Madam, I'm Adam!                 | true   |
      TODO (Cucumber::Pending)
      ./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
      features\palindrome.feature:6:in `Given the input "<input>"'
      | Abracadabra                      | false  |
      TODO (Cucumber::Pending)
      ./features/step_definitions/palindrome_steps.rb:2:in `/^the input "([^"]*)"$/'
      features\palindrome.feature:6:in `Given the input "<input>"'

3 scenarios (3 pending)
9 steps (6 skipped, 3 pending)
0m0.010s
5) Improve the palindrome_steps file...final version
Code:
require_relative 'palindrome.rb'

Given /^the input "([^"]*)"$/ do |input|
  @input = input
end

When /^palindrome\? is run$/ do
  @output = palindrome?(@input).to_s
end

Then /^the output should be "([^"]*)"$/ do |expected_output|
  raise('Wrong Output') unless @output == expected_output
end
And the corresponding palindrome.rb file, spoilered because it's part of the homework
Spoiler:
Code:
def palindrome?(string)
  pretty_string = string.downcase.gsub(/\W/,'')
  return  pretty_string == pretty_string.reverse
end


Obviously you'll get there by getting closer to getting the tests to pass step by step (i.e. your first palindrome? could have simply returned true)

Code:
Feature: Detect Palindromes

  As a user I want to be able to check if a given word or phrase is a palindrome.

  Scenario Outline: Provide input to palindrome? # features\palindrome.feature:5
    Given the input "<input>"                    # features/step_definitions/palindrome_steps.rb:3
    When palindrome? is run                      # features/step_definitions/palindrome_steps.rb:7
    Then the output should be "<output>"         # features/step_definitions/palindrome_steps.rb:11

    Examples:
      | input                            | output |
      | A man, a plan, a canal -- Panama | true   |
      | Madam, I'm Adam!                 | true   |
      | Abracadabra                      | false  |

3 scenarios (3 passed)
9 steps (9 passed)
0m0.015s
GREAT SUCCESS!

(I'll answer questions as to how/why this works etc if you want)
tl;dr versin:
1) Create a feature file describing your feature and providing some scenarios (scenario outline I used is an advanced concept that lets you use a table). Special keywords: Feature, Scenario, Scenario Outline, Given, When, Then (and some others)
2) The steps are basically regexp matching to get you some variables to use
3) Implement code

Documentation:
http://cuke4ninja.com/toc.html

NOTE: Works with other languages as well, not just Ruby

Last edited by clowntable; 02-29-2012 at 03:33 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:19 PM
Not sure if it's been posted already (nothing came up from search) but this might be of interest to some people here:

http://www.bbc.co.uk/news/technology-17196115 (a bit more info on the Wiki page: http://en.wikipedia.org/wiki/Raspberry_Pi)

Juk
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 03:22 PM
Rasperry Pi does look cool, I want to order one
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 04:04 PM
Quote:
Originally Posted by Gullanian
Rasperry Pi does look cool, I want to order one
Already on the waiting list Want one really badly!! Just hope they can ship it out to Canada.

Its a seriously cool piece of kit and should make computing much more accessible for schools.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
02-29-2012 , 04:06 PM
And Gullanian makes it to front page of HN again!! God I hate you!!


Edit: Ok, I take that back, its an excellent article. Really going point about how cookies are always sent in the request so using a 2nd domain reduces request size... never thought of that.

Last edited by MrWooster; 02-29-2012 at 04:13 PM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m