Page 1 of 1

How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:22 pm
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?

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:26 pm
by Brf
You have not shown us where you have defined $cal, $a, or $b

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:28 pm
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).

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:30 pm
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>

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:32 pm
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.

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 8:47 pm
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.

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 9:09 pm
by Brf
"is_set" only returns true or false. You want "variable".

Re: How to replace $_POST with $request?

Posted: Mon Jul 17, 2017 9:16 pm
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);
}

Re: How to replace $_POST with $request?

Posted: Tue Jul 18, 2017 9:24 am
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.

How to get POST results without page reloading?

Posted: Tue Jul 18, 2017 9:47 am
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.

Re: How to get POST results without page reloading?

Posted: Tue Jul 18, 2017 12:01 pm
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 -->