How to replace $_POST with $request?

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

How to replace $_POST with $request?

Post by infinitiv »

I had a

Code: Select all

$c = new calc();

if(isset($_POST['submit']))
{ 
$result = $c->getresult($_POST['a'], $_POST['b']);
}
which worked fine. Changed it to work with phpBB using:

Code: Select all

$request->is_set('a');
$request->is_set('b');
$c = new calc();

if ($request->is_set_post('submit'))
{ 
$result = $c->getresult($a, $b);
}
now $a and $b returns NULL. What did I do wrong?
Last edited by infinitiv on Mon Jul 17, 2017 8:34 pm, edited 3 times in total.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53398
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How to replace $_POST with $request?

Post by Brf »

You have not shown us where you have defined $cal, $a, or $b
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

Re: How to replace $_POST with $request?

Post by infinitiv »

Brf wrote: Mon Jul 17, 2017 8:26 pm You have not shown us where you have defined $cal, $a, or $b
Do I? I posted the only lines that I’ve changed. Everything works fine in PHP fiddle, but not in phpBB (after including common.php and such).
Last edited by infinitiv on Mon Jul 17, 2017 8:35 pm, edited 1 time in total.
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

Re: How to replace $_POST with $request?

Post by infinitiv »

$a and $b should be pulled from the form in the template. It goes like this:

Code: Select all

<form method="post">

<tr>
<td>A:</td>
<td><select name="a">
<option value="123">123</option>

</select></td>
</tr> 
<tr>
<td>B:</td>
<td><input type="text" name="b"></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="submit" value="CALCULATE"></td>
</tr>
<tr>
<td><strong><?php echo $result; ?><strong></td>
</tr>
</table>
</form>
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

Re: How to replace $_POST with $request?

Post by infinitiv »

There were some mistakes in the code which aren’t present on my board. For example there’s no „$cal” you asked about. I was editing it to show off here on phpbb.com and I got clumsy. Sorry. I already edited first post.
Last edited by infinitiv on Mon Jul 17, 2017 8:50 pm, edited 1 time in total.
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

Re: How to replace $_POST with $request?

Post by infinitiv »

OK, so I managed to fix that with:

Code: Select all

$a = $request->is_set('a');
$b = $request->is_set('b');
$c = new calc();

if ($request->is_set_post('submit'))
{ 
$result = $c->getresult($a, $b);
var_dump ($a, $b);
}
But hitting the „submit” button just reloads site nad var_dump gives me „bool(true)”. It didn’t outside of phpBB with $_POST.
User avatar
Brf
Support Team Member
Support Team Member
Posts: 53398
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Re: How to replace $_POST with $request?

Post by Brf »

"is_set" only returns true or false. You want "variable".
User avatar
RMcGirr83
Former Team Member
Posts: 22016
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: How to replace $_POST with $request?

Post by RMcGirr83 »

Code: Select all

$a = $request->variable('a', 0);// 0 for int ' '  for string	
$b = $request->variable('b', 0);// 0 for int ' '  for string	
$c = new calc();

if ($request->is_set_post('submit'))
{ 
	$result = $c->getresult($a, $b);
	var_dump ($a, $b);
}
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beer Image
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

Re: How to replace $_POST with $request?

Post by infinitiv »

Thank you all very much, the code’s now working and I gained necessary knowledge to move forward with my edits. One thing concerns me though, I can’t seem to assign block to a template. Now i have:

Code: Select all

$a = $request->variable('a', ' ');
$b = $request->variable('b', 0);

if ($request->is_set_post('submit'))
{ 
	if ($a == 'something')
	{
		$result = 'The A is '. $a .' and '. $b .' is the B';
	}
}

$template->assign_block_vars('somename', array(
      'RESULT' => $result,
   ));
   var_dump ($result);
The template is just a table with very simple form:

Code: Select all

<table>
	<tr>
		<td>
			A:
		</td>
		<td>
			<select name="a">
				<option value="something">something</option>
			</select>
		</td>
	</tr> 
	<tr>
		<td>
			B:
		</td>
		<td>
			<input type="text" name="b">
		</td>
	</tr>
	<tr>
		<td>
			<input type="submit" name="submit" value="Get a result">
		</td>
	</tr>
	<tr>
		<td>
			Result is:  
			<li>
				{somename.RESULT}
			</li>
		</td>
	</tr>
</table>
„var_dump ($result);” gives me the correct values but nothing shows in the template where „{somename.RESULT}” is. I tried to move „$template->assign_block_vars” inside of my IF with no avail.
infinitiv
Registered User
Posts: 166
Joined: Sat Nov 15, 2014 3:47 pm
Location: PL
Name: Ficjusz

How to get POST results without page reloading?

Post by infinitiv »

…Turned out that you cannot actually use „$template->assign_block_vars” when there’s only a single variable. I’ve used „$template->assign_var” instead.
Now everything works fine, but I need to find a way to get a result without having the page to reload.
User avatar
javiexin
Code Contributor
Posts: 1157
Joined: Wed Oct 12, 2011 11:46 pm
Location: Madrid, Spain
Name: Javier
Contact:

Re: How to get POST results without page reloading?

Post by javiexin »

infinitiv wrote: Tue Jul 18, 2017 9:47 am …Turned out that you cannot actually use „$template->assign_block_vars” when there’s only a single variable. I’ve used „$template->assign_var” instead.
Now everything works fine, but I need to find a way to get a result without having the page to reload.
Yes, you can use assign_block_vars with whatever number of vars (yes, 0 and 1 as well).
But to access them, you have to "loop" through each block of vars... You have to use <!-- BEGIN somename -->
Post Reply

Return to “phpBB Custom Coding”