-4

I don't understand how to send the form variables using isset when we click on submit. When i click the submit button, a notice is appearing indicating:

Undefined index: rollno in C:\xampp\htdocs\test7.php on line 8.

My intention is to update the details by clicking on submit and update using isset function and after updating return to the form again.

My code is here:

 <html>
  <head>
   <style>
   </style>
  </head>
  <body>
    <form method="post" action="test7.php">
       <center>Enter Roll Number: <input type="text" name="rollno" required/></center><br>
          <center><input type="submit" name="submit" value="submit" id="submit" /></center>
   </form>
  </body>
</html>

test7.php starts here

 <html>
     <head>
     </head>
     <body>
       <?php
         include('connect.php');
         $rno=$_POST['rollno'];
          $sql="select * from stddetails where rollno='$rno'";
          $query=mysql_query($sql);
          $count=mysql_num_rows($query);
          if($count>0)
          {
             $row=mysql_fetch_array($query);

          }
        ?>
        <center><form method='post'>
            <table>
                <tr><td>Roll Number</td><td><input type='text' name='roll' value='<?php echo $row['rollno'];  ?>'  disabled /></td></tr>
                <tr><td>Student Name</td><td><input type='text' name='sname' value='<?php echo $row['sname'];  ?>' size='80' /></td></tr>
                <tr><td>Father Name</td><td><input type='text' name='fname' value='<?php echo $row['fname'];  ?>' size='80'/></td></tr>
                <tr><td>gender</td><td><input type='text' name='gender' value='<?php echo $row['gender'];  ?>' /> </td></tr>
                <tr><td>Aadhar</td><td><input type='text' name='aadhar' value='<?php echo $row['aadhar'];  ?>' /> </td></tr>
                <tr><td>SSC Htno</td><td><input type='text' name='ssc' value='<?php echo $row['sschtno'];  ?>' /></td></tr>
                <tr><td>SSC board</td><td><input type='text' name='board' value='<?php echo $row['sscboard']; ?>' /></td></tr>
                <tr><td>SSC Grade</td><td><input type='text' name='grade' value='<?php echo $row['sscgrade'];  ?>' /></td></tr>
                <tr><td colspan='2'><center><input type='submit' name='Submit' value='Update' /></center></td></tr>
           </table>
        </form></center>

      <?php
         if(isset($_POST['Submit']))
       {
          echo"success";
       }

      ?>
     </body>
    </html>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
PAVAN
  • 85
  • 1
  • 1
  • 8
  • 2
    **The `mysql` PHP extension is dead** -- Stop using the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php). They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Jan 11 '18 at 09:51
  • It's true but at work, sometimes we can't choose to use PDO because of legacy code... e.g. the boss tell to do that – user2342558 Jan 11 '18 at 09:53
  • Above my paygrade to convince your boss to go along with time and update his stuff... wait i dont get paid at all ... so my advise is for free :) – Dorvalla Jan 11 '18 at 11:54
  • Related: https://stackoverflow.com/questions/58143519/why-is-isset-postlike-not-working-when-i-click-on-submit-button – Jesse Nickles Jul 01 '22 at 20:07

3 Answers3

1

on line 8 you must write:

$rno = isset($_POST['rollno']) ? $_POST['rollno'] : '';

instead of:

$rno=$_POST['rollno'];

That ternary operator (condition) ? true : false set the value for $rno after checked if the variable $_POST['rollno'] exists.

user2342558
  • 5,567
  • 5
  • 33
  • 54
  • Thanks for your help. undefined rno is rectified. when i entered the form and click submit button it is posting to form in test7.php. But when i click the update button in form in test7.php the inputs in the form are filled with
    Notice: Undefined variable: row in C:\xampp\htdocs\test7.php on line 24
    – PAVAN Jan 11 '18 at 09:56
  • Because `if($count>0) { $row=mysql_fetch_array($query); }` isn't necessary. Simple write `$row=mysql_fetch_array($query);` Then, the `$row` variable will be empty if there will be no data. – user2342558 Jan 11 '18 at 10:00
  • Thank you my friend. my problem is solved – PAVAN Jan 11 '18 at 10:02
  • another doubt please. in the form in test7.php all the inputs varaibles are getting by post method to isset except roll again. it was showing undefined variable here also. – PAVAN Jan 11 '18 at 10:35
  • See your code ` Roll Number' disabled />` change from roll to rollno – user2342558 Jan 11 '18 at 10:40
  • sorry i didnt get it – PAVAN Jan 11 '18 at 10:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162990/discussion-between-pavan-and-user2342558). – PAVAN Jan 11 '18 at 13:57
-1

// try this ..

 if (isset($_POST["submit"]) && !empty($_POST["submit"])) {
        echo "Success";    
    }else{  
        echo "Unsuccess";
    }
Nimesh Patel
  • 796
  • 1
  • 7
  • 23
  • While this code may answer the question, providing additional context regarding **how** and **why** it solves the problem would improve the answer's long-term value. – Alexander Jan 11 '18 at 12:48
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – yivi Jan 11 '18 at 14:30
-1

Just as an explination.

Technically what happens is that the code is already being excecuted when you load the page. There is no check to tell it when it should happen. Thats what the if(isset()) is for. It checks, as it states, that if there are variables present or that a certain action is happened, such as the submit button. Since your button is needed to do an update, you tell it, as @User2342558 states to check if that action has happened. If so, then excecute your piece of code. When the check isn't happening, your variables will be empty and causes problems.

Dorvalla
  • 5,027
  • 4
  • 28
  • 45