Can't get my form to pass an ID

Discussion in 'PHP' started by tyankee, Mar 10, 2014.

  1. #1
    I'm a newbie to programming forms but i have this form coding:

    <form name="traininglist" id="onsite" method="post" action="<?php $_SERVER['PHP_SELF']; ?>" style="margin:3px 0px 5px 10px;padding:0px;float:left;">
      <select name="training_id" style="width:275px;height:30px;">
                             <option selected>Please select one</option>  
      <?php
      $result=mysql_query("SELECT * FROM training ORDER BY activity");
      while($row=mysql_fetch_array($result))
      {
         echo '<option class="lbOn" value="training_form.php&id='.$row['training_id'].'" '.$hstyle.'>'.$row["activity"].'</option>';
      }
      ?>
      </select>
      <!--  <?php dateOptions(''); ?>  --> 
      
      </form>
    Code (markup):
    I want it to pass 'id' to the program training_form.php. The value at the time of the call to training_form.php seems to be filled in correctly when i do a view source on that link. But when it get to the subprogram training_form.php, the 'id' field appears to be blank.

    Here is the coding in the training_form.php program:

    <?php
    session_start();
    require_once('cfg/config.php');
    session($_SESSION['id']);
    mysql_select_db(DATABASE, mysql_connect(HOSTNAME,USERNAME,PASSWORD));
    $gettrain=mysql_query("SELECT * FROM training WHERE training_id='".$_POST['id']."' ") or die(mysql_error());
    $rowtrain=mysql_fetch_array($gettrain);
    // at this point i tried to display $rowtrain and it is blank.  If it try to access training_form.php //all by itself with the &id parameter i get page not found.  for example www.website.com/training_form.php&id=9 - returns a page not found.
    ?>
    <form name="training_form" method="get" id="training_form" action="<?php $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="id" value="<?php echo $rowtrain['training_id']; ?>" />
    
    <div id="servinghead">Add to Today's Exercise</div>
    <div id="servingtitle"><?php echo substr($rowtrain['activity'],0,35); ?></div>
    <div id="servingform">
    <div style="padding-bottom:5px;"></div>
    
    <table width="100%" border="0">
       <tr>
         <td>Enter Duration:</td>
      </tr>
      <tr>
         <td><input type="text" name="duration" size="2" onkeyup="ajaxercise(this.value,this.id)" id="<?php echo $rowtrain['training_id']; ?>" /><span style="font-size:80%">(minutes)</span></td>
      </tr>
      <tr>
         <td><span id="msg"></span></td>
      </tr>
      <tr>
         <td style="text-indent:0px;"><input style="margin-left:15px;" name="training" type="submit" value="Submit"  /><a href="#" class="lbAction" rel="deactivate"><button>Cancel</button></a></td>
      </tr>
    </table>
    </div>
    <div id="servingtitle"></div>
    <div id="servinghead"></div>
    </form>
    <?php mysql_close(); ?>
    Code (markup):

    any help would be greatly appreciated.
     
    tyankee, Mar 10, 2014 IP
  2. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #2
    I think you need to update the value of the Option with ID only see below

    Currently you have
    echo '<option class="lbOn" value="training_form.php&id='.$row['training_id'].'" '.$hstyle.'>'.$row["activity"].'</option>'
    Code (markup):
    Please replace with...
    echo '<option class="lbOn" value="'.$row['training_id'].'" '.$hstyle.'>'.$row["activity"].'</option>';
    Code (markup):
    So you will have the id in training_id variable.
     
    salmanshafiq, Mar 12, 2014 IP
  3. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #3
    actually it was the & that was the problem.. instead of &id, it should have been ?id
     
    tyankee, Mar 12, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    @salmanshafiq has it right, you don't put a get style query in as the value of a OPTION, that's just gibberish. I'd also suggest not using string addition and instead using delimits so it's a hair faster and more predictable, getting rid of the classes on the OPTION since those cannot accept CSS styling (by the specification, gecko is WRONG when it allows it), and likewise that $hstyle variable is likely also not doing anything in any browser other than FF. I'd probably also get that placeholder style option out of the select and make it a label pointing AT the select.

    Of course axing the tables for layout, getting some VALID and proper form code with things like FIELDSETs and LABEL's in there, getting rid of the pointless NAME attribute on the FORM itself (unless you REALLY insist on coding decade and a half out of date nyetscape 4 style JS)... and of course all those blasted inlined STYLE tags that just makes your PHP have to work harder building the output...

    Since you're a nube, what source are you learning from? My advice, forget it, you've already built some really nasty habits there; much of it being decade and a half out of date practices. From the non-semantic presentational markup to the use of the long deprecated mysql_ functions, you've got some learning to do.

    I mean, you've even got the database login info in DEFINE, the most insecure of methods no matter what the re-re's at turdpress say about it...
     
    deathshadow, Mar 12, 2014 IP
  5. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #5
    It shouldn't be either, unless you are doing something REALLY jacked up with the result.

    Just:
    value="', $row['training_id'], '"

    Nothing more. There's no legitimate reason to waste sending a URL as the value; that makes no sense whatsoever.
     
    deathshadow, Mar 12, 2014 IP
  6. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #6
    yikes.. some people are just born angry i guess. you make some valid points but this is NOT my coding - it's existing coding that i inherited.. and the style parameters DO work.
     
    tyankee, Mar 12, 2014 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    It's not about whether or not it's working, it's about whether or not it's right - working does not equal right, nor good practice - learning correct ways of doing things before trying to do non-standard is a good starting point.
     
    PoPSiCLe, Mar 13, 2014 IP
  8. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #8
    i agree but it's hard to listen to angry people.. that's not a good way to educate someone - it just turns them off..
     
    tyankee, Mar 13, 2014 IP
  9. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #9
    the URL is so that when they click on the option, it takes them to another program.. is there a better way to do that?
     
    tyankee, Mar 13, 2014 IP
  10. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #10
    Assuming that it's not an actual form submit and nothing else is being sent for data (which is what 99% of the time this 'scripting for nothing' does on things like forums) -- they're called ANCHORS. Javascript + select to do the job of a list of anchors with a hover state is NOT good coding.

    Even if it is VERY commonplace for people to waste javascript on making sure it's inaccessible with no graceful degradation. I'd have to see the whole page live, but there is likely little if any legitimate reason for the code of that page to be much more than:

    echo '
    	<div id="onsite">
    		<h2>Please select one</h2>
    		<ul>';
    		
      $result=mysql_query("SELECT * FROM training ORDER BY activity");
      while ($row=mysql_fetch_array($result)) echo '
    		<li>
    			<a href="training_form.php&id=', $row['training_id'], '">
    				', $row["activity"], '
    			</a>
    		</li>';
    		
    	echo '
    		</ul>
    	<!-- #onsite --></div>';
    Code (markup):
    Then it doesn't need any scripting; you want the dropdown, do it with CSS hover states and :target

    SMF forums does the same script-tardery with it's quick-link; every time I see this particular bit of nonsense I really wonder if the author understood enough HTML/CSS to be writing anything... but then SO many people seem to be diving for JavaScript to do things that can be done better without it these days. It's like the constant use of redundant inline style PHP dev's are CONVINCED are necessary, when 99.99% of the time it's just code bloat.
     
    deathshadow, Mar 14, 2014 IP
    tyankee likes this.
  11. tyankee

    tyankee Well-Known Member

    Messages:
    1,023
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    150
    #11
    thank y
    thank you.
     
    tyankee, Mar 14, 2014 IP