I am relatively a newbie to PHP. I have been able to get a page to list all my tasks in a form from my mysql database. However now I want to add a textbox against each task with some notes, and then have a button which I can click after adding the notes. Then on posting a submit, I want to add this info to my database.

Currently when I submit the form, the data is coming as blank...and hence I am unable to add the info.

My code snippet for listing the tasks in my php is:

if (isset($POST['submitted']) && !empty($POST['amount'])) {
//add to database
}

print "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=post>\n";

//get the info from database...in $task_details, task_id, $date, $time

print "<div class=box3><table width=\"100%\" border=\"0\"><tr><td width=\"90%\">".
"<table width=\"99%\" border=\"0\" ><tr><td>".
$task_details.
"</td></tr><tr><td>It needs to be done on <b>".$date."</b> at <b>".$time."</b></td></tr></table>".
"</td><td width=\"10%\">".
"<table width=\"99%\" border=\"0\"><tr><td>".
"<input type=text name=amount size=10 tabindex=1></input></td></tr>".
"<tr><td>".

"<input name=\"submitted\" type=\"hidden\" value=\"true\" />".
"<input type=submit value=\"Submit\" name=".$task_id."></input></td></tr></table></td></tr></table></div>";

Am I doing something impossible, or is there a better way to do this? Have been struggling with this for a few days now, and hence seeking help.

    First thing I see that's wrong is that you're using tables where proper HTML and CSS would be much better. The 1990s are over, y'know. 😉

    Second, I'm not really sure what it is you're asking... especially given that you've hidden much information from us. For example, it's anyone's guess as to what the code replaced by "//add to database" really does (which may or may not include adding something to a database). Same goes with "//get the info from database".

    Next, are you trying to be able to edit multiple entries from a single page, or is there only going to be one task per page? If it's the former, you could either use separate <form>'s for each task, or simply give each submit button some unique value. It looks like you're currently using "$task_id" for the value of those buttons; an alternative could be something like:

    <input type="submit" name="submit[123]" value="Edit Task">

    where "123" is that same task ID value (which is now going to appear as an index in the array $_POST['submit'], leaving the actual "value" of that button to be something more meaningful to the end user as far as describing what that button actually does).

    Also, when posting PHP code, please use the board's [noparse]

    ..

    [/noparse] bbcode tags as they make your code much easier to read and analyze.

      Brad,
      First - Thanks for responding so quickly...
      Second - Sorry for two things...
      - for primitive coding...
      - for not having the whole code. Did not want to flood the message with my code.

      My goal is
      - to list all the tasks with a comment-box and submit against each...if possible in one form
      - but accept comments only for one task when the form is submitted

      Here is the complete code I have written that does not work:

      <?php

      // Open db connection
      openDB();

      if (isset($POST['submitted']) && !empty($POST[''])) {

      //Execute sql
      if (isset($_POST['task_id'])) {
      	$task_id = (int) $_POST['task_id'];
      } else {
      	$task_id = 0;
      }
      
      // Escape the task:
      // Assumes Magic Quotes are off!
      $notes = mysql_real_escape_string($_POST['notes']);
      
      $queryStr = "INSERT INTO task_notes (task_id, notes) VALUES ($task_id, $posting_user_id, $user_id, $notes)";    
      
      $resultSet = mysql_query($queryStr);
      
      // Report on the results:
      if ($resultSet != true) {
        echo '<p>The task could not be added!</p>';
      } else {
        echo '<p>The task has been added!</p>';
      }
      $_POST['notes']=null;

      }

      ?>
      <?php
      print "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=post>\n";
      ?>
      <td class="main"><div class="main1" width=620>

      <div class=center>
      <div class=center>
      <b><i>My tasks</i></b>
      </div>
      </div>
      <?php

      // Retrieve all the uncompleted tasks:
      $query = 'SELECT task_id, task_details, activity_date FROM tasks WHERE task.status <> 1';
      $resultSet = mysql_query($query);
      if (!$resultSet) { // add this check.
      die('Invalid query: ' . mysql_error());
      }

      // Also store the tasks in an array for use later:
      $posts = array();

      while (list($task_id, $task_details,$activity_date) = mysql_fetch_array($resultSet, MYSQLI_NUM)) {

      $datetime = strtotime($activity_date);
      $date = date('M j', $datetime);
      $time = date('g:i a', $datetime);

      print "<div class=box3><table width=\"100%\" border=\"0\"><tr><td width=\"90%\">".
      "<table width=\"99%\" border=\"0\" ><tr><td>".
      $task_details.
      "</td></tr><tr><td>It needs to be done on <b>".$date."</b> at <b>".$time."</b></td></tr></table>".
      "</td><td width=\"10%\">".
      "<table width=\"99%\" border=\"0\"><tr><td>".
      "<input type=text name=notes size=10 tabindex=1></input></td></tr>".
      "<tr><td>".

      "<input name=\"submitted\" type=\"hidden\" value=\"true\" />".
      "<input type=submit value=\"Submit\" name=".$task_id."></input></td></tr></table></td></tr></table></div>";

      }
      ?>
      </div></td>

      </div></td></form>
      <?php

      closeDB();

      ?>

        bradgrafelman didn't ask for the whole code, he asked

        Also, when posting PHP code, please use the board's [noparse]

        ..

        [/noparse] bbcode tags as they make your code much easier to read and analyze.

        You were right to not want to flood the message with code - chances are that 90% of it is irrelevant to the problem and the other 10% would be lost in the noise (being able to isolate that 10% for posting is of course a useful skill when debugging). Unfortunately, if "adding the info" is the problem, removing the code that is supposed to "add the info" doesn't really help.

        I see a problem in this line:

        $queryStr = "INSERT INTO task_notes (task_id, notes) VALUES ($task_id, $posting_user_id, $user_id, $notes)"; 

        [font=monospace]$notes[/font] is a string value, and SQL needs quotes around it to make a valid query (another use of

        if (!$resultSet) { // add this check.
        die('Invalid query: ' . mysql_error());
        }

        would have alterted you to this). I also notice that you're trying to insert four values into two columns.

        Better still, don't use the outdated MySQL extension, use one of the more modern [man]MySQL[/man] extensions instead: then you'd be able to used parameterised queries and have the database driver do all the necessary quoting and escaping.

          Trying to use the

          ..

          in this post

          I have still not got to the sql errors, since when I submit my form...the values that I submit are all displayed as null.

           if (isset($_POST['submitted']) && !empty($_POST[''])) { 
          

          So have I passed the values correctly in the following?

          print "<div class=box3><table width=\"100%\" border=\"0\"><tr><td width=\"90%\">".
          "<table width=\"99%\" border=\"0\" ><tr><td>".
          $task_details.
          "</td></tr><tr><td>It needs to be done on <b>".$date."</b> at <b>".$time."</b></td></tr></table>".
          "</td><td width=\"10%\">".
          "<table width=\"99%\" border=\"0\"><tr><td>".
          "<input type=text name=notes size=10 tabindex=1></input></td></tr>".
          "<tr><td>". 
          "<input name=\"submitted\" type=\"hidden\" value=\"true\" />".
          "<input type=submit value=\"Submit\" name=".$task_id."></input></td></tr></table></td></tr></table></div>";
          

            Not sure if my cry for help got lost...

            Like you can see in the previous code snippet,
            I have been able to get a page to list all my tasks in a form from my mysql database and add a textbox against each task alongwith a button which I can click after adding the notes. Then on posting a submit, I want to add this info to my database.

            Currently when I submit the form, the data is coming as blank...and hence I am unable to add the info. So is the code to have multiple buttons within a form, right?

              jazzatbay;11011355 wrote:
               if (!empty($_POST[''])) { 
              

              Do you have an array element named '' ? Otherwise it will of course be empty...

              You can inspect the contents of $_POST (or any array) with

              # CLI, or serving content to browser with mime type text/plain
              print_r($_POST,1);
              
              # web page (using mime type text/html or similar)
              printf('<pre>%s</pre>', print_r($_POST,1));
              

                Yes, I corrected my code as follows...But it still does not go into the "if" code section

                *if*(isset($_POST['submitted'])*&&*!empty($_POST['notes']))*{* 
                

                Since my hidden variable has same value for all rows, should I use different hidden values for each of them...probably
                corresponding to
                task_id....is that the solution?

                  Thanks to Brad and another post...I got this working...

                    22 days later

                    Give different name of both submit button..

                      6 days later
                      bradgrafelman;11011333 wrote:

                      First thing I see that's wrong is that you're using tables where proper HTML and CSS would be much better. The 1990s are over, y'know. 😉

                      Well I am not really sure if I agree with this, the code may look quite messy with lots of php <? and ?> tags in a html document(considering there may be javascript too). If a html form contains quite a bit of php variables, I'd personally prefer using php alone like the OP did so you do not have to switch from html to php frequently. Perhaps it's just me and a few programmers though.

                        Lord Yggdrasill;11013403 wrote:

                        the code may look quite messy with lots of php <? and ?> tags in a html document

                        I personally keep HTML markup (and CSS, JavaScript, etc.) out of my PHP files for this reason (and several others). (And besides, once you add PHP code to the mix, it's no longer an "html document" anyway - it's a PHP script which ultimately produces an HTML document. 😉)

                          bradgrafelman;11013405 wrote:

                          I personally keep HTML markup (and CSS, JavaScript, etc.) out of my PHP files for this reason (and several others). (And besides, once you add PHP code to the mix, it's no longer an "html document" anyway - it's a PHP script which ultimately produces an HTML document. 😉)

                          Yeah thats true, I have trouble reading code with PHP, Javascript, HTML and CSS altogether, even with just two of them is difficult for me. XD It's a good idea to keep php file with php-code only and minimize the number of php tags used in your html file. Javascript can be kept away from html with the <script> tag, which is nice too.

                            bradgrafelman;11013405 wrote:

                            I personally keep HTML markup (and CSS, JavaScript, etc.) out of my PHP files for this reason (and several others). (And besides, once you add PHP code to the mix, it's no longer an "html document" anyway - it's a PHP script which ultimately produces an HTML document. 😉)

                            Yeah thats true, I have trouble reading code with PHP, Javascript, HTML and CSS altogether, even with just two of them is difficult for me. XD It's a good idea to keep php file with php-code only and minimize the number of php tags used in your html file. Javascript can be kept away from html with the <script> tag, which is nice too.

                              Lord Yggdrasill wrote:
                              bradgrafelman wrote:

                              First thing I see that's wrong is that you're using tables where proper HTML and CSS would be much better. The 1990s are over, y'know.

                              Well I am not really sure if I agree with this, the code may look quite messy with lots of php <? and ?> tags in a html document

                              ...But whether or not you have <?...?> in the document doesn't have that much to do with whether the HTML looks like it dates from last century.

                                Weedpacket;11013423 wrote:

                                ...But whether or not you have <?...?> in the document doesn't have that much to do with whether the HTML looks like it dates from last century.

                                Yeah thats true, I was just stating that reading tens of php <? and?> tags in html document(mostly in html tables and forms) gives me headache. I know some people are actually quite comfortable with this, not me though.

                                  Weedpacket;11013423 wrote:

                                  ...from last century.

                                  It's funny because it's true!

                                    Write a Reply...