-1
<form method="post">
<input type="submit" name="'.$pTest.'" value="submit" id="submit"/>
</form>

<?php
if(isset($_POST[''.$pTest.'']))
{echo 'something'
;}
?>

This doesn't work if I use variable as the name of the submit button. Please some help!

dggdsdg
  • 27
  • 2
  • 2
  • 3
  • Why the empty strings around the variable in the $_POST check? It's not the problem, but it's useless. – naththedeveloper Oct 13 '14 at 08:55
  • That syntax is incorrect. Have a look at how to insert a PHP variable inside HTML. Just one of the many examples: [php variable in html no other way then: ``](http://stackoverflow.com/questions/2150238/php-variable-in-html-no-other-way-then-php-echo-var) – Max Oct 13 '14 at 08:55

4 Answers4

0

Try like this

<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
DRK
  • 160
  • 1
  • 4
  • 14
0

Your variable is doing nothing. Just echo your <input> name and catch it whith php:

<form method="post">
   <input type="submit" name="<?php echo $pTest ?>" value="submit" id="submit"/>
</form>

<?php

    if (isset($_POST[$pTest])) {

        echo 'something'

    }

?>
alquist42
  • 739
  • 1
  • 8
  • 21
0

Add value to the variable first..

<?php  $pTest = 'sks'; ?>
<form method="post">
<input type="submit" method="post" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
</form>

<?php
if(isset($_POST[$pTest]))
{
    echo '<script>alert("something");</script>';
}
?>
Sanket
  • 36
  • 1
  • 8
-2

Just replace your quotes with double quotes :

if(isset($_POST["'.$pTest.'"]))
{
    echo 'something'
;}

Or name your input with a PHP variable :

<input type="submit" name="<?php echo $pTest; ?>" value="submit" id="submit"/>
Adysone
  • 121
  • 1
  • 8
  • 1
    You honestly believe that he wants to name the submit button: `'.$pTest.'`? – Max Oct 13 '14 at 08:58
  • Nope, but if he wants, he can :) – Adysone Oct 13 '14 at 09:02
  • He can but it doesn't mean he should. Encouraging best practices while answering a question is much more valuable than blindly answering a question just for the sake of a few rep points. – Max Oct 13 '14 at 09:31
  • You're right, that's why I edited my post with a real answer ;) – Adysone Oct 14 '14 at 16:42