Jump to content


Language Snippet not doing what I expect


7 replies to this topic

#1 prdriskell

    Etomite Forum Newbie

  • Member
  • 6 posts

Posted 23 November 2009 - 04:15 PM

Dear all,

I have successfull created versions of the ListDocs snippets that filter in only documents starting with EN_ or PL_ or IT_ etc. based on $_SESSION["Lingo"] set manually.

What I thought would be a simple PHP snippet to set $_SESSION["Lingo"] just isn't working! For me, it correctly shows the form , then when I press Submit, Etomite displays my home page, and the variable is not set.

I know that I can set $_SESSION["Lingo"] from within a snippet, but my code never takes me there. Please tell me what I am doing wrong, before my head explodes!

Snippet "Lingo"
//tried several different Ifs
//if(!isset($_POST["LingoSelect_submit"])) 
if(!isset($_POST["Lingo"]))

{
$output ="";
$output .="Language is currently set to ".$_SESSION["Lingo"]."
";
$output .="Please choose a new language
";

$output .="<form id=\"LingoSelect\" action=\"\" method=\"post\" >";
$output .="<div><table>";
$output .="<input type=\"hidden\" name=\"id\" value=\"\" />";
$output .="<tr><td><input type=\"radio\" name=\"Lingo\" value=\"en\" /></td><td>English</td></tr>";
$output .="<tr><td><input type=\"radio\" name=\"Lingo\" value=\"it\" /></td><td>Italian</td></tr>";
$output .="<tr><td><input type=\"radio\" name=\"Lingo\" value=\"pl\" /></td><td>Polish</td></tr>";
$output .="<tr><td><input type=\"radio\" name=\"Lingo\" value=\"fr\" /></td><td>French</td></tr>";
$output .="<tr><td style=\"text-align:center;\">";
$output .="<input type=\"submit\" id=\"LingoSelect_submit\" name=\"LingoSelect_submit\" value=\"Submit\" class=\"button\" /></td><td></td>";
$output .="</tr></table></div></form>";
return stripslashes($output);
}

else

{
$output ="";
$_SESSION["Lingo"]=$_POST["Lingo"];
$output .="Your language is now " .$_SESSION["Lingo"];
return $output;
}

Lots of room for improvement...

Edited by Ralph, 25 November 2009 - 04:29 PM.
Enclosed snippet code within code tags...


#2 vw53a

    Likes Etomite Forums!

  • Member
  • PipPip
  • 393 posts

Posted 23 November 2009 - 10:50 PM

I haven't been coding for a while now, but my first hunch is that it's in the <form action=""> part where "" should be (required) a valid url. It may make sense that "" will send you to your homepage. But that's just my 10p.

#3 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,506 posts
  • Gender:Male

Posted 23 November 2009 - 10:54 PM

Two ideas come to mind and both are related to caching... First, does the snippet work if you use non-cached snippet tags...??? By that I mean using [!Lingo!] instead of [[Lingo]]... The next thing to verify is that the document containing the snippet call is not flagged to be cached... Of course, if the snippet is located in the template you can disregard the document caching idea...

Hope this helps...


@vw53a, if action is null the current page will be reloaded on submit...

#4 prdriskell

    Etomite Forum Newbie

  • Member
  • 6 posts

Posted 24 November 2009 - 11:38 AM

The cache doesn't seem to be the problem.

I decided to take a different approach to solve my problem, so I looked through published snippets that involve generating a form and processing it's contents, and quickly saw Ralph's excellent Authenticate_visitor snippet.

Using the "Boiled Spinnach" approach to programming (reducing many lines of code to the bare essentials, testing that the core functionallity had not been compromised), I ended up with a working SetLingo snippet which I diffidently include here. Why this works, and the original doesn't, may remain a mystery until production deadlines allow for some headscratching time.

Ralph's "form_class.php" contains enough clues, even though it is undocumented. Very useful.

Thanks all.


// SetLingo snippet - usage [!SetLingo!]

if(isset($_POST['submit']))
{
 // get only the forms $_POST variables we want based on the prefix "frm_"
 $fields = $etomite->getFormVars($method="POST",$prefix="frm_",$trim=1,$REQUEST_METHOD);
 // extract the variable array into plain variables
 extract($fields);

 if (isset($Lingo)) $_SESSION["Lingo"]=$Lingo;
 return $Lingo." is set" ;
}

// if all else fails, generate the user authentication form
else
{

 // assign miscellaneous variables
 $_submitText = "Submit";
 $formVar = array();
 
 // create all required form elements using the form class
 $form = new formClass();
 
 // create the <form> element
 // As I don't do any field validation, not sure I need the Return v.exec(), but left it in cos Ralf saiys to!!
 $_openform = $form->openform(array(
	'id'=>'Select Language',
	'action'=>'',
	'method'=>'post',
	'onsubmit'=>'return v.exec()'
	)
 );
 
 // define default name attribute prefix
 $form->namePrefix="frm_";
 
 // define default label name prefix
 $form->labelSuffix="_lbl";
 
 // END: optional snippet call redirect parameters

 // English input tag
 $_LingoEn = $form->input(array(
	'type'=>'radio',
	'id'=>'LingoEn',
	'name'=>'Lingo',
	'value'=>'En',
	'label'=>array(
 	'id'=>'LingoEn',
 	'for'=>'LingoEn',
 	'label'=>'English:'
 	)
	)
 );
 
 // French input tag
 $_LingoFr = $form->input(array(
	'type'=>'radio',
	'id'=>'LingoFr',
	'name'=>'Lingo',
	'value'=>'Fr',
	'label'=>array(
 	'id'=>'LingoFr',
 	'for'=>'LingoFr',
 	'label'=>'French:'
 	)
	)
 );
 
 // form data submit button
 $_submit = $form->input(array(
	'type'=>'submit',
	'id'=>'submit',
	'name'=>'submit',
	'value'=>$_submitText,
	'class'=>'button'
	)
 );
 
 // end of field generation

 // create the </form> element
 $_closeform = $form->closeform();
 
 // extract the form labels created by the form class
 extract($form->formLabels);
 
 // get any tigra forms validation rules
 $rules = $form->tigraGetRules();

// render the form using simple template technique
$output .= <<<END
$rules
 $_openform
	<div class="loginForm">
 	<table class="loginTable">
 	<tr>
 	<th colspan="2">Select your Language</th>
 	</tr>
 	<tr>
 	<td>$LingoEn_lbl</td>
 	<td>$_LingoEn</td>
 	</tr>
 	<tr>
 	<td>$LingoFr_lbl</td>
 	<td>$_LingoFr</td>
 	</tr>
 	<tr>
 	<td colspan="2" class="alignCenter">
 	$_submit
 	</td>
 	</tr>
 	</table>
	</div>
 $_closeform
\n
END;

 // return to caller for display
 return stripslashes($output);
}
// THE END

Edited by Ralph, 24 November 2009 - 07:10 PM.
Enclosed snippet code within code tags...


#5 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,506 posts
  • Gender:Male

Posted 24 November 2009 - 07:04 PM

Glad to hear that you got things working as needed... And while the form class is a neat feature I only use to occasion myself... It does allow for a more progamatic method of maintaining code, however... These days I usually have my snippet call a chunk which contains the form markup... You can omit the validation code as you haven't set any form validation rules... Also, no white spaces in ID names...

Here is the minimalistic code I would use to tackle this task... Simple and easy to maintain, especially when not input validation is required... As you can see I have made English the default selection but this could be changed easily... Your current code is perfectly acceptable, however... Just goes to show that theres always more than one solution to a problem...

// Snippet Name: SetLingo snippet
// Usage: [!SetLingo!]

if(!isset($_SESSION['Lingo']))
{
 if(isset($_POST['submit']))
 {
	$_SESSION["Lingo"] = $_POST['Lingo'];
	return $_SESSION['Lingo']." is set" ;
 }
 else
 {
	return $etomite->getChunk("Lingo");
 }
}
else
{
 return $_SESSION['Lingo']." is set" ;
}

<!-- Chunk Name: Lingo -->
<form id="SelectLanguage" action="" method="post">
<div class="loginForm">
 <table class="loginTable">
	<tr>
 	<th colspan="2">Select your Language</th>
	</tr>
	<tr> <!-- set English as the default language selection -->
 	<td><label id="LingoEn" for="LingoEn">English:</label></td>
 	<td><input type="radio" id="LingoEn" name="Lingo" value="En" checked="checked" /></td>
	</tr>
	<tr>
 	<td><label id="LingoFr" for="LingoFr">French:</label></td>
 	<td><input type="radio" id="LingoFr" name="Lingo" value="Fr" /></td>
	</tr>
	<tr>
 	<td colspan="2" class="alignCenter">
 	<input type="submit" id="submit" name="submit" value="Submit" class="button" />
	</td>
	</tr>
 </table>
</div>
</form>

Edited by Ralph, 25 November 2009 - 04:41 PM.
Fixed minor bugs in both the snippet and chunk code.


#6 Cris D.

    Loves Etomite Forums!

  • Developers
  • PipPipPipPip
  • 1,104 posts
  • Gender:Male

Posted 25 November 2009 - 09:18 AM

View PostRalph, on 23 November 2009 - 10:54 PM, said:

@vw53a, if action is null the current page will be reloaded on submit...
Actually, not alway Ralph. I've had multiple issues with forms not posting back to the source page if a form action is null. The safest way is to pass

form action=".$etomite->makeURL($etomite->documentIdentifier,'','')."

otherwise as desscribed the form posts back to the home page on some servers.

#7 Ralph

    Loves Etomite Forums!

  • Admin
  • 6,506 posts
  • Gender:Male

Posted 25 November 2009 - 02:11 PM

True, Cris... Indeed, I have seen servers that didn't like a null action... The markup won't validate without an action either, depending on DTD used...


Another method of populating the action attribute would be to use:

action="[~id~]" which causes the parser to create a URL using FURL's (reliable)

OR

action="[*id*]"
which causes the parser to use the page id only (not as reliable)

As usual, multiple ways to accomplish most tasks within Etomite...

#8 prdriskell

    Etomite Forum Newbie

  • Member
  • 6 posts

Posted 26 November 2009 - 11:36 AM

Ralph, your Chunk based solution posted on 24th is a thing of beauty. It works on my system without even the need for action="[~id~]", just action="".

Simplified the snippet still futher for my own needs, as I need it to always offer the option to set a language.

 
if(isset($_POST['submit']))
{
    	$_SESSION["Lingo"] = $_POST['Lingo'];
    	return $_SESSION['Lingo']." is set" ;
}
else
{    	
    	return $etomite->getChunk("Lingo");
}

Thanks.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users