0

Hello i have a strange problem i have the following form

echo "<td  class='contact-delete'>
 <form method='post' action='update_record.php'>";
echo "<button type='submit' value='" . $row['ID_PER'] . "' name='recordID' id = 'recordID'>edit</button>";
echo "</form>";

in the update_record.php I have the following code

$id2update = $_POST['recordID'];
echo $id2update ;

session_start();


if(!$_SESSION['logged']){
   header("refresh:3; url=start_page.php" );}

// above this part of code there is an other form with a submit button   
else if(isset($_POST['submit'])){
   echo $id2update ;
}

The problem is that the first echo outputs the variable as it supposed but the second echo does not output anything it is null. Can anyone give me an explanation?

edit: The 2nd echo is being called but the value is null!

9
  • Are you sure $_SESSION['logged'] is set and not null? Do you have error reporting on (E_ALL )? Commented Jul 27, 2014 at 9:42
  • Are you sure that the second echo is called at all? Mayb the condition is never met. Throw some more var_dumps in between to see how far you code is executed. This question smells like bad debugging… Commented Jul 27, 2014 at 9:42
  • There is nothing named 'submit' in the form. Commented Jul 27, 2014 at 9:46
  • @feeela by using var_dump i get null. Something deletes my variable and i can not understand what. Commented Jul 27, 2014 at 9:47
  • What are you dumping? Commented Jul 27, 2014 at 9:48

1 Answer 1

1

You say "above this part of code there is an other form with a submit button". I assume it is that you are trying to echo with the second submit? In that case the data is never submitted. When you submit an HTML form only that form is submitted. Any inputs/buttons/etc in other forms on the page are not sent to the server and so not available in the PHP code. This is by design.

Thus your code as it stands should never reach the second echo.

(Technically only non-disabled form elements with non-null name in the current form are submitted)

Edit: I think I now understand the comment in your code. You have one form in another file which submits to update_record.php In that request you render a new form (as part of update_record.php) with a submit button. That new form submits to itself with the submit button. If this is correct then the point is that the submit of the form from update_record.php (the one with the submit button) is a new request. You set the value of $id2update in one request but then do a new request and in that it is not set. You should include that value as a hidden input in the form rendered in update_record.php:

<input type="hidden" name="recordID" value="<?php echo $id2update;?>" />

then, when the second request is made (when the update_record.php form is submitted) the vlaue will be fetched again.

Each request must be treated for itself - the server does not automatically know which requests go together (up to data saved in the session of course).

Sign up to request clarification or add additional context in comments.

1 Comment

in the update_record.php i have a second form and the submit works fine, the problem is that when i click submit i lose the value of $id2update from the original form (that called the update_record.php in the first place). Is there a work around so that i can keep both?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.