开发者

How to get the value from a form in php

<? 
   if('POST' == $_SERVER['REQUEST_METHOD']
   {
       $_SESSION['update'] = trim($_POST("wasUpdateClicked")_;
   }
   $wasUpdatedClicked = $_SESSION['wasUpdatedClicked'];
   if(isset($wasUpdateClicked))
   {
      ..do something here like an update method
   }
?>
<form name="x" method="post" action="samePage.php">
      ...do some stuff here that isnt important
   <input type="submit" value="update" onclick="$wasUpdateClicked = 开发者_C百科2" />
</form>

My beef is that I cant get the right value for the wasUpdateClicked variable. Is there any way to do this???? It seems that when my action is to the same page I can't get my variables. Please help me!!!


$_POST is a variable, not a function.

Subscript it like so...

$var = $_POST['wasUpdateClicked'];

Also, I usually find it easier to check for a POST like with if ( ! empty($_POST)) { ... }.

To use $_SESSION, you need to call session_start() first.

This line has a few issues...

<input type="submit" value="update" onclick="$wasUpdateClicked = 2" />

You can not do that with PHP. I think you are confusing JavaScript with PHP (still, that would be bad practice JavaScript).


In addition to @alex's answer:

This is not how forms work. Only the content of form elements are sent to the server, identified by their name.

So this

<input type="submit" value="update" onclick="$wasUpdateClicked = 2" />

will just set the JavaScript variable $wasUpdateClicked to 2. This happens only on the client side. The value is never sent to the server.

You would need a form element, like:

<input type="text" name="wasUpdateClicked" value="2" />
<!-- or type="hidden" -->

and maybe update this value with JavaScript. Or if you actually want to count the number of form submissions, you can count them on the server side using $_SESSION (just having a form element that always sends 2 seems kinda useless).

You should read about variables from external sources and describe what you actually want to do.


You shall replace

 <input type="submit" value="update" onclick="$wasUpdateClicked = 2" />  

by

<input type="hidden" name="wasUpdateClicked" value="2"/>  
<input type="submit" value="update"/>  

and at the beg. of your script, get the posted value with

$wasUpdatedClicked = $_POST['wasUpdateClicked'];  


There are many ways to do this. Here are some pointers which should help.

If you assign a name to a button, its value will be part of the $_POST array.

 <input type="submit" name="submit" value="update"></input>
 <input type="submit" name="submit" value="submit"></input>

Depending on which button they clicked, $_POST['submit'] will be "update" or "submit".

Another way to do this is to set a hidden input's value in the onclick function.

<input type="hidden" id="action" name="action" value="">
<input type="submit" value="update" onclick="document.getElementById('action').value = 'update'" />

Then, $_POST['action'] will be "update" if they clicked the update button.

To do the update if they clicked the update button, do something like this:

if (isset($_POST['action']) && $_POST['action'] == 'update')
{
    // do your update
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜