2

Hi While login into the site iam getting the username in hidden field while filling the form after login i need to insert the username in database.But iam getting The error as

Notice: Undefined index: email

Here is my code

index.php

<?php  
$username = $_SESSION['username'];
if($username) 
{ 
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php 
}  
else 
{ 
echo "no"; 
} 
?> 
<body>       
    <input type='hidden' value="<?php echo $username; ?>" name='email'>        
    <input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />    
    <form method="post" action="details.php" id="myform">            
        <ul class="tab-content">
                <li class="tab-pane active" id="salary">                
                            <h3>Details</h3>
                            <table style="border-collapse: collapse;border: 1px solid black;">
                            <tr class="spaces">
                              <th>User Name</th></tr>
                              <tr>
                              <td><input type="text" name="user_name" value="" required /></td>
                              </tr>

details.php

<?php
$connection = mysql_connect("localhost", "root", "") or die(mysql_error());
$db = mysql_select_db("accountant", $connection);
$email=$_POST['email'];
$name=$_POST['user_name'];
$query=mysql_query("INSERT INTO user_details(email,user_name)values('$email','$name')");

Can anyone help me this thanks in advance

While filling this form i need to insert the hidden field value also in database

1
  • First of all you need to place the hidden input field inside the Form. Then the value will be sent together with other data. And from there you can access the value as other values inside the form. (which you have already done as i can see). Commented Feb 3, 2016 at 12:34

6 Answers 6

1

You hidden field must be inside your form . Otherwise its value not submit form action and you will get Undefined Notice

<form method="post" action="details.php" id="myform"> 
 <input type='hidden' value="<?php echo $username; ?>" name='email'>      

--- rest code---

 <input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />  

</form>
Sign up to request clarification or add additional context in comments.

Comments

1

You are getting Undefined Index notice because you are not using your hidden input inside the <form></form>

<input type='hidden' value="<?php echo $username; ?>" name='email'>

Keep in mind either filed hidden or visible you will define it inside the <form></form> otherwise you will get the Notices.

Side Note:

Please use mysqli_* or PDO extension, because mysql_* is deprecated and not available in PHP 7.

One more issue, your submit button also out side the <form> but it will work because you are using onclick() event, but its not good practice.

Your code is open for SQL Injection, you need to prevent with SQL Injection.

One last thing, i hope you are using session_start() on top in your file for getting $_SESSION['username']; in your original code, please recheck.

Comments

0

Try putting that field in form tag and you done.

<?php
    $username = $_SESSION['username'];
    if($username){?>
        <h3> Welcome <?php echo $username; ?></h3>
    <?php } else {
        echo "no";
    }
?>
<body>
    <input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />
    <form method="post" action="details.php" id="myform">
        <input type='hidden' value="<?php echo $username; ?>" name='email'>
        <ul class="tab-content">
            <li class="tab-pane active" id="salary">
                <h3>Details</h3>
                <table style="border-collapse: collapse;border: 1px solid black;">
                    <tr class="spaces">
                        <th>User Name</th>
                    </tr>
                    <tr>
                        <td>
                            <input type="text" name="user_name" value="" required />
                        </td>
                    </tr>

1 Comment

Thanks..If my answer was helpful then accept it by clicking right marker and give up vote. This is the best way to appreciate the answer.
0

index.php

<?php  
$username = $_SESSION['username'];
if($username) 
{ 
?>
<h3> Welcome <?php echo $username; ?></h3>
<?php 
}  
else 
{ 
echo "no"; 
} 
?> 
<body>       

<input type="button" value="Logout" id="logout" onClick="document.location.href='login.php'" />    
<form method="post" action="details.php" id="myform">            
    <ul class="tab-content">
            <li class="tab-pane active" id="salary">
  <input type='hidden' value="<?php echo $username; ?>" name='email'>                 
                        <h3>Details</h3>
                        <table style="border-collapse: collapse;border: 1px solid black;">
                        <tr class="spaces">
                          <th>User Name</th></tr>
                          <tr>
                          <td><input type="text" name="user_name" value="" required /></td>
                          </tr>

Comments

0
<?php
  session_start();
  // you have to start the session in order to work with php session.  
  $username = $_SESSION['username'];  
?>

Comments

0
> <form method="post" action="details.php" id="myform">   <input
> type='hidden' value="<?php echo $username; ?>" name='email'>      
> 
>  <input type="button" value="Logout" id="logout"
> onClick="document.location.href='login.php'" />  
> 
> </form>

hidden type always contain hidden value

Comments

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.