Jump to content

form code expansion help please


paulmo

Recommended Posts

Based on "engage" section of linked page below, need to expand processed greeting to feature a time of day and theme-specific (dependent on radio field selection) greeting, followed by an article which would correspond to the radio-selected theme. Ex: "It's a grey morning Jane..." followed by text about gray weather. get/post/loop/while/for etc are terms i've been reading about and i'm guessing they'll figure into this expansion. if someone could please steer me in the direction to get started—much appreciated! barely familiar with mysql, have created a table previously and would like to incorporate a database into this as well. but just to get the basic idea down would be a start. thanks http://releasecenter.org/interactive.php

Code:<?php//Check whether the form has been submittedif (array_key_exists('check_submit', $_POST)){   //Converts the new line characters (\n) in the text area into HTML line breaks (the <br /> tag)   $_POST['Comments'] = nl2br($_POST['Comments']);    //Check whether a $_GET['Languages'] is set   if ( isset($_POST['Colors']) ) { 	 $_POST['Colors'] = implode(', ', $_POST['Colors']); //Converts an array into a single string   }   //Let's now print out the received values in the browser   echo "Greetings {$_POST['Name']},";      echo " you are feeling {$_POST['theme']} at the moment.<br />";} else {

Link to comment
Share on other sites

  • Replies 54
  • Created
  • Last Reply

The first thing I notice is that you didn't close your else on the bottom line, which PHP should complain about when it parses that page (if the if is false, at least).So I (mis?)understand that you want the message to be more dynamic. This looks like a good application of switch, which has this syntax:

switch($var){	case 'value1':		// This will be executed ONLY if $var == 'value1'.		break;	case 'value2': case 'value3':		// This will be executed ONLY if $var == 'value2' OR if $var == 'value3'.		break;	default:		// This will be executed ONLY if all the other cases fail.  (Always put this at the bottom or it will hide any cases below it.)}

Only the first matching case will execute, even if an identical one appears after the first match.In your case (no pun intended), it would be something like this:

$time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 18 ? 'afternoon' : 'evening';//^These^ are 2 shorthand 'if's, known as "ternary (three-part) statements" or "conditional statements."// $destinationVar = $trueOrFalse ? 'if true' : 'if false';switch($_POST['theme']){	case 'grey':		echo "It's a grey {$time} {$_POST['Name']}...";		break;	case 'black':		// Do something similar here.		break;	case 'light':		// Again.		break;	default:		echo "Stop trying to hack my application, {$_POST['Name']}!";}

Link to comment
Share on other sites

jesdisciple, thanks so much for taking the time to put that code together and explain it—it works! but the time is off, as it's 7 am here and greetings say "afternoon." i googled the $time variable and it seems the (date) should be reading local time? also if you'd please explain what the $destinationVar if true/false part means. also if you'd please suggest 1) how to incorporate another form and variables into the processing? 2) ideas of how to use this with mysql? thanks! paul

$time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 18 ? 'afternoon' : 'evening';//^These^ are 2 shorthand 'if's, known as "ternary (three-part) statements" or "conditional statements."// $destinationVar = $trueOrFalse ? 'if true' : 'if false';switch($_POST['theme']){

Link to comment
Share on other sites

i googled the $time variable and it seems the (date) should be reading local time?
No - PHP only knows server time.
also if you'd please explain what the $destinationVar if true/false part means.
Its commented out, so... nothing.
1) how to incorporate another form and variables into the processing? 2) ideas of how to use this with mysql?
Well... it depends on what you want to do. The correct order is Have Idea, then Design, then Develop, not Develop, then Have Idea :)
Link to comment
Share on other sites

thanks synook, so i'll rephrase: how to read local not server time? if commented out, why include $destinationVar if true/false?1) how to incorporate another form and variables into the processing? 2) ideas of how to use this with mysql?1= relates to code...want to know what the process may be integrated with what jesdisciple's already suggested. 2= mysql integrated with different information for variables? (only used mysql for a comments form where messages were posted...other than that clueless how it could add to application)

Link to comment
Share on other sites

$destinationVar was a demonstration of a simple ternary statement to help you understand the more complex arrangement above it.JavaScript knows local time. You could inject PHP variables into JavaScript, then process the time-dependent stuff and output the PHP variables - but only in JavaScript-enabled and -capable browsers.I don't know that MySQL would be very useful here, because you only have three (or four, including the default) strings that you want to output. Unless you want to have several sets of these, I think it'd be a wasted database.What do you mean by "another form and variables"? What exactly are you trying to do, or is this just exercise/education?EDIT: A quick Google didn't find much about detecting local time with PHP, unless you can either 1) get the user to tell you their time zone or 2) use JavaScript to retrieve it (i.e. by AJAX, hidden form field, etc.).EDIT2: Save this code with a PHP extension, but make sure you recognize it as JavaScript by its name. For example, I like to use the form "filename.js.php". Note that I haven't tested it so you should. (Also, if you're not going to use MySQL at all in this project, it would be best to go with pure JavaScript.)




			
		
Link to comment
Share on other sites

I don't know that MySQL would be very useful here, because you only have three (or four, including the default) strings that you want to output. Unless you want to have several sets of these, I think it'd be a wasted database.What do you mean by "another form and variables"? What exactly are you trying to do, or is this just exercise/education?
Chris thanks again—too bad no PHP for local time, but if js is the answer i'll try your code. are you saying use only js not php for site? yes i'm planning to have several sets of strings that will connect to database. this is self-taught (with w3schools) personal project. thanks! paul
Link to comment
Share on other sites

I was saying no PHP, but since you need MySQL that's not possible.Here's an idea; see how far you can go with programming it. In the form where the user enters their theme and name, include a hidden field "time_offset" or the like. Use PHP to put the server hour (<?php date('G') ?>) in that hidden field when the form-page is served. Use JavaScript to calculate [the local hour] minus [the server hour] and put it back in the hidden field. When the form is submitted, you can know the user's time by adding the server time to $_POST['time_offset'] and the output doesn't need any JavaScript.(I tell you to calculate "time_offset" on the client side because the user may have loaded the page several hours before submitting the form, so the period between when the page is served and when the page is loaded is the shortest possible.)(It might be better to calculate the difference between the full time, because some time zones might have strange offsets.)BTW, I do this kind of project too. :)

Link to comment
Share on other sites

chris i'm seeing some js to read local time as var today = myDate.getHours() and wondering how do i use that in the php file you set up to process form? obviously those $time variables will not work being replaced by myDate.getHours. do i add the js to the html/form page instead? then how to link switch statements in php page with js time on form page?

Link to comment
Share on other sites

You can postback the JS variable to PHP via AJAX. (or you could do all the time of day calculations in JS and just document.write() the result to the page)

Link to comment
Share on other sites

Sorry I was slow to see this.On the page where the user inputs the other data, have PHP insert a hidden field with a value of date('G'). When the page loads, use JavaScript to get the hours, subtract the PHP hours from them, and put the result back in the hidden field. The JavaScript would look like this (use <body onload="main();">):

function main(){	var hours = (new Date()).getHours();	var offset = document.getElementById('offset');	offset.value = hours - offset.value;}

The user submits the form and you have their time offset.

Link to comment
Share on other sites

hello chris, haven't played with javascript yet to get local time, but i will soon. where do i put this?: <body onload="main();"> have been reviewing algebra for a test recently and thinking about trying to write a php algebra script for a trinomial or quadratic equation. figure it might be good practice for something else. any pointers? thanks for your help

Link to comment
Share on other sites

<body onload="main();">
You replace your body tag with that code.
Link to comment
Share on other sites

thanks synook would that be the form page or the page that processes the form?also phase 2 of interactive center needs the following: season- (month range) dependent greeting and another 3-variable radio button field that will interact with first radio field and post/print text greeting. Putting it all together it would generate something like this for example: 1) time (in place now) 2) month range (ex. nov-march="winter") 3) gray 4) "alone" (choices will also include family/friends in 2nd radio field): "A dreary winter morning weighs down on you Paul, and you should get out in public to elevate your spirit; you can always return to introspection." according to my math, 6 variables= 9 possible (perhaps with month/seasons as well) textual variances.as it is now i've got the time and gray/black/light field; my goal now seems more complex. help or direction again? thanks--

Link to comment
Share on other sites

thanks synook would that be the form page or the page that processes the form?
That's the form page. Remember that the code interacts with a form element. Basically, the time data piggybacks on the visible form to achieve an AJAX-like effect.EDIT: I just found and edited a bug in my code. The second line in the function shouldn't contain ".value".
also phase 2 of interactive center needs the following: season- (month range) dependent greeting
That could be a switch statement. Because I would rather you write the code, I'll try to only describe it. Use the syntax I gave above in the first switch example to make [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] each have one effect as values of the month. (Unless you plan to be precise and use the official beginning of each season, months should be rounded up; e.g. winter began Dec 22, '07, so fall approximately extends through December.)
and another 3-variable radio button field that will interact with first radio field and post/print text greeting. Putting it all together it would generate something like this for example: 1) time (in place now) 2) month range (ex. nov-march="winter") 3) gray 4) "alone" (choices will also include family/friends in 2nd radio field): "A dreary winter morning weighs down on you Paul, and you should get out in public to elevate your spirit; you can always return to introspection."
This might be better suited to checkboxes, because family and friends may be present at one time. No checked boxes would mean "alone." (However, 2 checkboxes would render 4 possibilities, 3*4*4=48.)
according to my math, 6 variables= 9 possible (perhaps with month/seasons as well) textual variances.as it is now i've got the time and gray/black/light field; my goal now seems more complex.
Yes, [3*3=9]*4=36. You have a lot of sentences to type up. :)
Link to comment
Share on other sites

My javascript local time correction and month/season variable code attempt results in: Parse error: parse error, unexpected T_VARIABLE in /home/content/p/m/o/pmollomo/html/process.php on line 26here's the sitereplaced <body> with <body onload="main();"> on form page:

<input type="radio" name="theme" value="grey" /> grey<input type="radio" name="theme" value="black" /> black<input type="radio" name="theme"  value="light" /> light  <input type="submit" value="Process" class="buttons"/><input type="hidden" function main(){	var hours = (new Date()).getHours();	var offset = document.getElementById('offset');	offset.value = hours - offset.value;}/></form>

php code (edited excerpt with time and season):

<?php$time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 18 ? 'afternoon' : 'evening';php$month = intval(date('G')) [1, 2, 3] ? 'winter' : intval(date('G')) [4, 5,] ? 'Spring' : intval(date('G')) [6, 7, 8] ? 'summer' : intval(date('G')) [9, 10, 11] ? 'harvest' : intval(date('G')) [12] ? 'Holiday Season';//^These^ are 2 shorthand 'if's, known as "ternary (three-part) statements" or "conditional statements."// $destinationVar = $trueOrFalse ? 'if true' : 'if false';switch($_POST['theme']){	case 'grey':		echo "A somber {$month} {$time} blankets your spirit, {$_POST['Name']}, but grey also provides an insulating comfort, calm, and reflection; be in it for a while and cherish its evenness.";		break;case 'black':	   echo "A difficult {$month} {$time}

along with help on this need to make text section contingent on season/month, as in if black & april, then "despite flowers blooming everywhere you are unable to see the color this morning." (not just state mood/time/month like "it's a dark Spring morning..." in other words these variables need to interact not just be stated. how to? thanks for help in this major advance.

Link to comment
Share on other sites

  • 2 weeks later...

Woops! I sure didn't know about this... paulmo, PM me next time I do that, please. :)main() should be in the head rather than the body. Also, where's the rest of your switch statement? (And of course, what's on line 26?)Synook, thanks... I probably wouldn't have gotten an email notification if you hadn't replied.

Link to comment
Share on other sites

Woops! I sure didn't know about this... paulmo, PM me next time I do that, please. :)main() should be in the head rather than the body. Also, where's the rest of your switch statement? (And of course, what's on line 26?)
i'm sorry chris, next time you do what? have moved maine() to head. maybe i'm missing something as it doesn't change anything except that code appears on the web page. line 26 (unsure how to count lines) is somewhere in switch statement. posted a section of it before, the rest of it isn't that exciting but here it is if it helps:
<?php$time = intval(date('G')) < 12 ? 'morning' : intval(date('G')) < 18 ? 'afternoon' : 'evening';php$month = intval(date('G')) [1, 2, 3] ? 'winter' : intval(date('G')) [4, 5,] ? 'Spring' : intval(date('G')) [6, 7, 8] ? 'summer' : intval(date('G')) [9, 10, 11] ? 'harvest' : intval(date('G')) [12] ? 'Holiday Season';//^These^ are 2 shorthand 'if's, known as "ternary (three-part) statements" or "conditional statements."// $destinationVar = $trueOrFalse ? 'if true' : 'if false';switch($_POST['theme']){	case 'grey':		echo "A somber {$month} {$time} blankets your spirit, {$_POST['Name']}, but grey also provides an insulating comfort, calm, and reflection; be in it for a while and cherish its evenness.";		break;	case 'black':	   echo "A difficult {$month} {$time} casts over your spirit at the moment, {$_POST['Name']}, yet before every breakthrough lies a challenge through darkness, anxiety and the unknown. Stay vigilant and prepare yourself for the return of joy, laughter, and saving light.";		break;	case 'light':		echo "The joy of light is upon you this {$month} {$time} {$_POST['Name']}; you are a living manifestation of satisfaction, confidence and love. You are merged with positive energy, and at the apex of living. Remember where you are now, as you will aspire to return here.";		break;	default:		echo "Please choose a theme, {$_POST['Name']}.";}?>

Link to comment
Share on other sites

You asked a question and I didn't know about it for 9 days. Next time I seem to ignore you like that, you should nudge me. :)main() should be in a script tag in the head.Re line number: What text editor are you using? If Notepad, go View > Status Bar or Edit > Go To... (odd - mine are grayed out). EDIT: If yours are grayed out, go Format > (uncheck) Word Wrap.

Link to comment
Share on other sites

ok i've got main() script tag in head, seems better:

<script type="text/javascript">function main(){	var hours = (new Date()).getHours();	var offset = document.getElementById('offset');	offset.value = hours - offset.value;}</script>

tried again...use notepad, line 26 is "line number out of range." couple things: like you, status bar is greyed out, but only with word wrap. with no word wrap, status bar selectable but shows no #'s. edit-go to also only works with no word wrap. word wrap seemed easier to work with. (had gotten into formatting trouble sending notepad code by e-mail to work remotely; re-saved/sent back, now it's mashed up but that's another topic i'm mostly concerned about code) thanks!

Link to comment
Share on other sites

Shows no #s?? Do you have your text-cursor (the | ) flashing? The status bar should show the line you're on - you change lines, it changes numbers. If it shows "Ln 26," you found your line.Also, text editors like PSPad and Notepad++ have an option to show line numbers next to the text (on the left).About word wrap, use Enter to put separate lines of code on separate lines. If some still run out of the screen, use your horizontal scrollbar.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.


×
×
  • Create New...