Jump to content

PHP posting problem


Gavski
Go to solution Solved by Gavski,

Recommended Posts

I have a form that contains a function. i want this function to be activated only when the user reposts the page to itself. (php_self).

 

ie. the user access the page and the function is hidden, the user then inputs data, repost the page and the function is activated.

 

I've tried flagging the function with a variable - $var = 0, then $var = 1 at the end of the page but $var just resets itself to 0 when the page is posted.

 

Sure this is simple but I just cant get it?

 

thanks

Link to comment
Share on other sites

Here's my form, it contains a function (really!). I want this function to work only after the form has been posted (not on initial loading). Here PHP reads the value of $token inside the function on loading (setsit to 1) and so executes the function when the page is loaded, there must be a way round this?

 

 

 

<?php

session_start();

$_SESSION['regName'] = $regValue; 

$token = 0;

//here's the function
function doThis()
{ echo "Do this Function";}


if ($token = 1)
{
doThis();
$token = 1;
}
echo $token;


?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<input type="text" name="regName" value="">
<input type="submit">
</form>

Link to comment
Share on other sites

Sorry here it is again without the glaring error - 

 

<?php

session_start();

$_SESSION['regName'] = $regValue;

$token = 0;
//here's the function
function doThis()
{ echo "Do this Function";}


if ($token = 1)
{
doThis();
}

$token = 1;
echo $token;


?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<input type="text" name="regName" value="">
<input type="submit">
</form>

Link to comment
Share on other sites

You could use a hidden field to run the function. For example:

 

<?php
function doThis() {
     echo "Do this Function";
}
 
if(isset($_POST['runFunction'])) {
     doThis();
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<input type="text" name="regName" value="">
<input type="hidden" name="runFunction" value="1">
<input type="submit">
</form>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.