7 Replies - 356 Views - Last Post: 07 August 2012 - 03:56 AM Rate Topic: -----

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

php echoing php?

Posted 06 August 2012 - 07:27 PM

So I have a form post returns another form which I would like to react to post again, or in other words, a form post output a form with clickable buttons. So I end up having something like

echo '<?php if(isset($_POST['button'])) echo "ouch!"?>';

or
echo "<?php if(isset($_POST['button'])) echo 'ouch!'?>";


i cannot avoid syntax error...

so i put the code in an external file??
<form action="$_SERVER[DOCUMENT_ROOT].'/wp-admin/external.php'">


but how do I pass $_POST['button'] into the external.php?

This post has been edited by e_i_pi: 06 August 2012 - 08:53 PM
Reason for edit:: Please use [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: php echoing php?

#2 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 751
  • View blog
  • Posts: 1,537
  • Joined: 30-January 09

Re: php echoing php?

Posted 06 August 2012 - 08:55 PM

That code isn't really enough to go on, and those echoes are definitely not going to work. I have the strong feeling that you're going about this entirely the wrong way. Could you post the entire code for your page so we can have a look and see what you're trying to do.

This post has been edited by e_i_pi: 06 August 2012 - 08:55 PM

Was This Post Helpful? 0
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: php echoing php?

Posted 06 August 2012 - 09:22 PM

My code is something like this:
<?php
if(isset($_POST['button'])) echo '<form method="post" action=....><table>';   (2nd form generated from 1st form, when a button is clicked)

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">  (1st form)
<input type="submit" name="button">
....
</form>


So I am trying to add action to the 2nd form (so another button on the 2nd form can be clicked too). I tried to echo '<?php...' because I thought the php-action-code needs to be present with the 2nd form? can I not echo php code from within php code?

I got the following to work, but it doesn't work quite right.. messed up my HTML table:
function make_deposit()
{
    if(isset($_POST['btnDeposit'])) echo $_POST['amtDeposit'];
}
echo '<form method="post" action="<?php make_deposit() ?>"><table>';


This post has been edited by e_i_pi: 06 August 2012 - 09:34 PM
Reason for edit:: Please use [code][/code] tags!

Was This Post Helpful? 0
  • +
  • -

#4 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 751
  • View blog
  • Posts: 1,537
  • Joined: 30-January 09

Re: php echoing php?

Posted 06 August 2012 - 09:44 PM

Could you please use code tags when you paste code, it makes it much easier to read.

You're going about this the wrong way. While your first piece of code should work fine, the second piece won't work. You can't assign a PHP function to a form action, as a form is client-side and PHP is server-side. You need to call a PHP page, either via a regular form submit, or via AJAX, then use the $_POST variable in PHP to retrieve the data that was submitted. You can't simply call a PHP function from a browser, you need to make a URL request to a server-side file.

EDIT: Have a read of this tutorial by no2pencil. It is based around the difference between $_GET and $POST, but has plenty of code and examples in there to show you how a form's HTML should look, and how to retrieve the posted values in the called PHP file.

This post has been edited by e_i_pi: 06 August 2012 - 09:54 PM

Was This Post Helpful? 1
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: php echoing php?

Posted 06 August 2012 - 10:15 PM

My 1st attempt was to send the php function along with the form (using echo). i still dont understand why that wouldnt work?
Was This Post Helpful? 0
  • +
  • -

#6 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 751
  • View blog
  • Posts: 1,537
  • Joined: 30-January 09

Re: php echoing php?

Posted 06 August 2012 - 11:25 PM

It won't work because the action attribute of a form can only take two things - a Javascript function, or a URL. The client-side has no visibility of the server-side functions.
Was This Post Helpful? 0
  • +
  • -

#7 Finale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-December 09

Re: php echoing php?

Posted 07 August 2012 - 01:12 AM

Here is a simple form to generate another form.
Maybe it can help.
<?php
$AnotherForm = "";

if(isset($_POST['btnDeposit'])) {
    $AnotherForm = "<form method='post' action='" . $_POST['amtDeposit'] . "'>
        <input type='submit' value='go to {$_POST['amtDeposit']}' />
</form>";  
}

$DocumentText = <<<HEREDOC
<html>
    <head></head>
    <body>
        <form action='{$_SERVER['PHP_SELF']}' method='post'>
            <input type='text' name='amtDeposit' value='go.htm' />
            <input type='submit' name='btnDeposit' value='submit' />
        </form>
        $AnotherForm
    </body>
</html>
HEREDOC;

echo $DocumentText;

?>


Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3115
  • View blog
  • Posts: 4,675
  • Joined: 08-June 10

Re: php echoing php?

Posted 07 August 2012 - 03:56 AM

View Postg37752, on 07 August 2012 - 05:15 AM, said:

My 1st attempt was to send the php function along with the form (using echo). i still dont understand why that wouldnt work?

View Poste_i_pi, on 07 August 2012 - 06:25 AM, said:

The client-side has no visibility of the server-side functions.

To elaborate a bit on the point e_i_pi is making there.

All PHP code is executed on the server computer, while the HTML and Javascript code is being processed by the browser on the client's computer.

If we take a look at your previous example:
echo '<form method="post" action="<?php make_deposit() ?>"><table>';


This will not work because you are sending PHP code to the browser. The browser has no idea how to deal with PHP code, only the server knows how to do that. So any PHP code you want executed needs to be executed on the server, long before the HTML and Javascript are processed.

So, again like e_i_pi says, if you want your HTML or Javascript code to execute a PHP function, you need to send a new request to the server in order for that to happen. Only a request for a PHP file can execute any PHP code.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1