Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-07-03 07:23:31

bsweb
New Member
Registered: 2014-07-03
Posts: 1

Problem with custom form

I have inherited a Textpattern website. I need to create a form. I have the following code, which works fine with standard php tags however using txp:php it seems to not make available the variables outside of the block of code. Please help.

<txp:php>
// define variables and set to empty values

if ($_SERVER["REQUEST_METHOD"] == "POST") {

	echo "GOT HERE";

	$Error = '0';

	if (empty($_POST["employername"])) {
		$employernameErr = "Employer Name is required";
		$Error = '1';
	} else {
		$employername = test_input($_POST["employername"]);
	}

	if ($Error == '1'){
		echo "Got Errors";		
	}

}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
</txp:php>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td valign="top">

			<form method="post" action="<txp:php> echo htmlspecialchars('/new-employee-details'); </txp:php>">
				<table>
					<tr>
						<td style="width:250px;">Employer Name (Compulsory) :</td>
						<td><input name="employername" type="text" value='<txp:php> echo $employername; </txp:php>' size="30"/><span class="error"><txp:php> echo $employernameErr; </txp:php></span></td>
					</tr>
				</table>


				<table>
					<tr>
						<td colspan="2" style="text-align:center;"><input type="submit" name="submit" value="Submit Form"> </td>
					</tr>
				</table>				
			</form>	
		</td>
	</tr>
</table>

In the above $employernameErr is set on post but not shown in the form.

Edit by gaekwad: added bc.. for code

Last edited by gaekwad (2014-07-03 07:27:52)

Offline

#2 2014-07-03 07:57:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Problem with custom form

bsweb wrote #281865:

using txp:php it seems to not make available the variables outside of the block of code.

That’s correct. You need to declare variables as global to share them between <txp:php> blocks in Textpattern.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#3 2014-07-03 13:31:12

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Problem with custom form

You might want to use txp own <txp:variable />, which are global:

...
parse('<txp:variable name="employername">'.test_input($_POST["employername"]).'</txp:variable>');
...

and

...
<input name="employername" type="text" value='<txp:variable name="employername" />' size="30" />
...

Offline

Board footer

Powered by FluxBB