Furkan Akgün
Furkan Akgün

Reputation: 15

How to POST data from value of input if its exist in echo

I have a input inside of <form action="include_php/update.inc.php" method="POST"> </form>like this

<input type="text" id="username" name="new-username" value="<?php echo $result['user_name']?>">
<input type="text" id="password" name="new-password" value="<?php echo $result['user_password']?>">

And i am trying to read value of this input to check something. Here is the Post Codes.

When i try to only update my password. It goes first if even if there is a data inside of it.

$userName=$_POST['new-username'];
$userPassword=$_POST['new-password'];
    if($_SESSION['userName'] !== $userName){
        $sql="UPDATE users SET user_name='$userName' WHERE user_id='$id'"; 
        $query=mysqli_query($conn,$sql); 
    }
    elseif($_SESSION['userPassword'] !== $userPassword){
         $sql="UPDATE users SET user_password='$userPassword' WHERE user_id='$id'"; 
         $query=mysqli_query($conn,$sql);
        header("Location:http://localhost/talktoworldx/Anasayfa/account/index.php#success=update");
    }

But $userName has already value from database but it return nothing. and it becomes like this if($_SESSION['userName'] !== "" )and it goes first if and change username to empty.

Maybe i am making really small mistake here but i couldnt figure out this.

So how can i send this value as POST method ?

Upvotes: 1

Views: 587

Answers (2)

patron
patron

Reputation: 59

try this and tell me the output :

  if(isset($_POST['new-username'])){

         $userName=$_POST['new-username'];  }

       else { $userName='default'; }

        if($_SESSION['userName'] !== $userName){
            $sql="UPDATE users SET user_name='$userName' WHERE user_id='$id'"; 
            $query=mysqli_query($conn,$sql);
      header("Location:http://localhost/talktowo
       rldx/Anasayfa/account/index.php#success=update");
      }
       else {
                echo 'something';
            }

Upvotes: 0

prasanth
prasanth

Reputation: 22500

Try with method attr of form set as post

<form method="post" action="url">
 <input type="text" id="username" name="new-username" value="<?php echo $result['user_name']?>">
</form>

FOR php

To validated the post data before

$userName=isset($_POST['new-username']) ? $_POST['new-username'] :''; 
if(!empty($userName){
 //do all stuff
}

Upvotes: 2

Related Questions