How to get value from 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 = 2" />
</form>

My beef is that I cannot get the correct value for the wasUpdateClicked variable. Is there any way to do this ???? It seems that when my action refers to the same page, I cannot get my variables. Please help me!

+3
source share
4 answers

In addition to @alex answer :

This is not how forms work. Only the contents of the form elements are sent to the server identified by them name.

So this is

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

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

You will need a form element, for example:

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

, , JavaScript. , , , $_SESSION ( , 2, ).

, .

+1

$_POST - , .

:

$var = $_POST['wasUpdateClicked'];

, POST if ( ! empty($_POST)) { ... }.

$_SESSION, session_start().

...

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

PHP. , JavaScript PHP ( , JavaScript).

+2

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

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

. script,

$wasUpdatedClicked = $_POST['wasUpdateClicked'];  
0

There are many ways to do this. Here are some pointers that 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 is pressed, it $_POST['submit']will be “updated” or “send”.

Another way to do this is to set the hidden input 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 it $_POST['action']will be “updated” if they clicked the update button.

To update by clicking the update button, do the following:

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

Source: https://habr.com/ru/post/1793736/


All Articles