Jump to content

Echoing the selected option from a populated list


kanpotter

Recommended Posts

I have a form that contains 3 text fields and 7 populated lists (that are dependent on the choice of the previous lists).  When I post the information to MySQL and to my processing page, it posts the table "ids" instead of the selected option. 

 

Here's the php code for the form:

 

<form action="gccsuccess_form.php" method="post" enctype="multipart/form-data" name="gcc" id="gcc">

  <table width="700" border="0" align="center" cellpadding="3" cellspacing="3">

    <tr>

      <td><label for="date">Today's Date:</label>

<input type="text" name="date" id="date"></td>

      <td> </td>

    </tr>

    <tr>

      <td>Week Number:

      <select name="week" size="1" id="week">

        <option value="0" selected>Week 0 - 3/26 - 4/1</option>

        <option value="1">Week 1 - 4/2 - 4/8</option>

        <option value="2">Week 2 - 4/9 - 4/15</option>

        <option value="3">Week 3 - 4/16 - 4/22</option>

        <option value="4">Week 4 - 4/23 - 4/29</option>

        <option value="5">Week 5 - 4/30 - 5/6</option>

        <option value="6">Week 6 - 5/7 - 5/13</option>

      </select></td>

      <td><label for="location"></label>

      </td>

    </tr>

    <tr>

      <td>Location:

        <select id='locations' name='location'>

      </select></td>

      <td> </td>

    </tr>

    <tr>

      <td><label for="label8">Team Name</label>

<select id='team_names' name='team_name'>

</select></td>

      <td> </td>

    </tr>

    <tr>

      <td><em>*Please enter each team member's first and last name:</em></td>

      <td> </td>

    </tr>

    <tr>

      <td><label for="mem_1"> Team <strong>Captain:</strong></label>

        <select id='team_members 1' name='capt'>

        </select></td>

      <td><label for="label4"><strong>Steps:</strong></label>

        <input type="text" name="steps_1" id="steps_1"></td>

    </tr>

    <tr>

      <td><label for="mem_2">Team Member #2 <strong>Name: 

        <select id='team_members 2' name='mem_2'>

        </select>

      </strong></label></td>

      <td><label for="label5"><strong>Steps:</strong></label>

        <input type="text" name="steps_2" id="steps_2"></td>

    </tr>

    <tr>

      <td><label for="mem_3">Team Member #3 <strong>Name:

            <select id='team_members 3' name='mem_3'>

        </select>

      </strong></label></td>

      <td><label for="label6"><strong>Steps:</strong></label>

        <input type="text" name="steps_3" id="steps_3"></td>

    </tr>

    <tr>

      <td><label for="mem_4">Team Member #4<strong> Name</strong>:

        <select id='team_members 4' name='mem_4'>

        </select>

      </label></td>

      <td><label for="label7"><strong>Steps:</strong></label>

        <input type="text" name="steps_4" id="steps_4"></td>

    </tr>

    <tr>

      <td><label for="label">Team Member #5<strong> Name</strong>:

        <select id='team_members 5' name='mem_5'>

        </select>

      </label></td>

      <td><label for="label7"><strong>Steps:</strong></label>

          <input type="text" name="steps_5" id="steps_5"></td>

    </tr>

    <tr>

      <td> </td>

      <td> </td>

    </tr>

    <tr>

      <td> </td>

      <td><em>* Maximum steps for each team member per week is <strong>105,000. </strong></em></td>

    </tr>

    <tr>

      <td> </td>

      <td><input type="submit" name="Submit Form" id="Submit Form" value="Submit"></td>

    </tr>

  </table>

  <label></label>

  <div align="center"><br>

    If you have questions about the competition or reporting please contact KC  WELLNESS, INC toll free at <SPAN id="lw_1237248942_0">877-634-1412.</SPAN><br>

    <br>

  </div>

</form>

 

 

And here's the code for the page it is posted in:

 

<?php

 

$date = $_POST['date'];

$week = $_POST['week'];

$location = $_POST['location'];

$team_name = $_POST['team_name'];

$capt = $_POST['capt'];

$mem_2 = $_POST['mem_2'];

$mem_3 = $_POST['mem_3'];

$mem_4 = $_POST['mem_4'];

$mem_5 = $_POST['mem_5'];

$steps_1= $_POST['steps_1'];

$steps_2= $_POST['steps_2'];

$steps_3= $_POST['steps_3'];

$steps_4 = $_POST['steps_4'];

$steps_5 = $_POST['steps_5'];

 

mysql_connect("mysql1.myregisteredsite.com", "49200_kandace", "Leonardo56")

or die(mysql_error());

mysql_select_db("49200_GCC") or die(mysql_error());

mysql_query("INSERT INTO `walkathon` VALUES ('$date', '$week', '$location', '$team_name', '$capt','$mem_2','$mem_3',  '$mem_4', '$mem_5', '$steps_1','$steps_2', '$steps_3','$steps_4', '$steps_5' )"); 

 

Print "Thank you for your entry. <br>

      Report summary: <br/><br/>";

 

echo "<strong> Date: </strong>  {$date} 

<br/> <br/><strong> Week: </strong>  {$week}

<br/> <br/><strong> Location: </strong>  {$location}

<br/> <br/><strong> Team Name: </strong>  {$team_name}

<br/> <br/><strong> Team Captain: </strong>  {$capt} / <strong> Steps: </strong> {$steps_1} 

<br /> <br/><strong> Team Member 2 Name: </strong>  {$mem_2} / <strong> Steps: </strong> {$steps_2} 

<br /> <br/><strong> Team Member 3 Name: </strong>  {$mem_3} / <strong> Steps: </strong> {$steps_3}

<br /> <br/><strong> Team Member 4 Name: </strong>  {$mem_4} / <strong> Steps: </strong> {$steps_4}

<br /> <br/><strong> Team Member 5 Name: </strong>  {$mem_5} / <strong> Steps: </strong> {$steps_5}"

 

?>

 

How do I get the processing page to echo the selected option instead of the id?

 

 

 

Link to comment
Share on other sites

Hi,

The $_POST['week'] can get only the data added in the "value" attribute of the selected option. You cannot get in php the text between <option>text</option>.

A solution could be to use value="id^text" , then, in php separate with explode('^', $_POST['week']) id and text.

Link to comment
Share on other sites

MarPlo,

 

Here's the actual page:  www.kcwell.com/gcc_form.php

 

Notice when you chose the options from "locations, team name, team captain and team members" and hit submit, they are not shown in the process page www.kcwell.com/gccsuccess_form.php.  It's posting the "ids" of the selections instead of the selections. 

 

Any suggestions?

Link to comment
Share on other sites

The PHP you are calling on to retrieve values via AJAX (connection1.php) is returning the wrong things. For example, after choosing "desplaines11" in the Team Name, the values for the rest of the dropdowns come back as desplaines11.

{"results":[{"text":"David Mecklenburger","value":"desplaines11"},{"text":"Gary Geppert","value":"desplaines11"},{"text":"David Chapa","value":"desplaines11"},{"text":"Kyle Fischer","value":"desplaines11"}]}

 

If you don't know what that means, post the code for connection1.php so we can help you.

Link to comment
Share on other sites

When you submit the form, the data is posted to the header and passed to the action page specified in the form tags; if none is specified it will stay on the current page. The information posted to the header is the "name" of each input/select etc which equals the value of the relevant input field. E.g.

 

<input type="text" name="hello">World</input>
[code]

This will post variables to the header which look like ?hello=world in its raw format. This is then put into your $_POST variable as $_POST = array("hello" => "World").

The same principle applies for select tags. The value specified in your option tag is the value posted to the header.  If you want to use IDs you need a table in your database which defines what the IDs link to. I assume this is already done as you retrieve the information as you progress through your drop downs.

Link to comment
Share on other sites

Here's the connection1.php:

 

<?php 

 

$hostname = "mysql1.myregisteredsite.com";

 

$database = "49200_GCC";

 

$username = "49200_kandace";

 

$password = "Leonardo56";

 

 

 

$query = array();

 

$query["locations"] = "SELECT id as value, location as text FROM locations";

 

$query["team_names"] = "SELECT DISTINCT team_name as text, id as value FROM team_names WHERE locations_id='%s'";

 

$query["team_members 1"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'";

 

$query["team_members 2"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'";

 

$query["team_members 3"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'";

 

$query["team_members 4"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'";

 

$query["team_members 5"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'";

 

 

 

include "db_manager.php";

 

 

 

?>

Link to comment
Share on other sites

Here's the dbmanager.php

 

<?php

 

// Routine to manage errors

function _error($errno, $errstr, $errfile, $errline){

print('{"error":{"message":"'.$errstr.'"}}');

set_error_handler("_error", E_USER_ERROR); // Set the error handler

 

// Routine to detect and create the json_encode function

if (!function_exists('json_encode')){

function json_encode($a=false){

if (is_null($a)) return 'null';

if ($a === false) return 'false';

if ($a === true) return 'true';

if (is_scalar($a))

{

  if (is_float($a))

  {

// Always use "." for floats.

return floatval(str_replace(",", ".", strval($a)));

  }

 

  if (is_string($a))

  {

static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));

return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';

  }

  else

return $a;

}

$isList = true;

for ($i = 0, reset($a); $i < count($a); $i++, next($a))

{

  if (key($a) !== $i)

  {

$isList = false;

break;

  }

}

$result = array();

if ($isList)

{

  foreach ($a as $v) $result[] = json_encode($v);

  return '[' . join(',', $result) . ']';

}

else

{

  foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);

  return '{' . join(',', $result) . '}';

}

} // End json_encode

}

 

$connection = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(), E_USER_ERROR);

mysql_select_db($database) or trigger_error(mysql_error(), E_USER_ERROR);

 

if(!empty($_REQUEST['get'])){

$query = $query[$_REQUEST['get']];

 

if(!empty($_REQUEST['id'])){

$query  = sprintf($query, mysql_real_escape_string($_REQUEST['id']));

}

 

$options = array();

 

if(!empty($query)){

 

$result = mysql_query($query, $connection);

if(mysql_num_rows($result)){

while($row = mysql_fetch_assoc($result)){

$options[] = $row;

}

}

}

 

print '{"results":'.json_encode($options).'}';

}

 

?>

Link to comment
Share on other sites

Sorry, it's kind of obvious now with just the first file. These 5 SQL queries are the same, so why would you expect different results?

$query["team_members 1"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'"; 

$query["team_members 2"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'"; 

$query["team_members 3"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'"; 

$query["team_members 4"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'"; 

$query["team_members 5"] = "SELECT DISTINCT team_member as text, team_names_id as value FROM `team_members` WHERE team_names_id='%s'"; 

Each one fetches something different, so they should be SELECTing different fields.

Link to comment
Share on other sites

That's not the problem.  I need the queries to be the same because a user needs to define 4-5 team members from a drop down menu.  Each of these menus have 4-5 options, just need to pick the option for each drop down and submit.   

 

Once the user has entered and/or chosen all the text on the form - www.kcwell.com/gcc_form.php I need all the chosen text to post to the mysql database and post on www.kcwell.com/gccsuccess_form.php.  Instead, the ID's are posting, not the selected options. 

 

Any ideas?

Link to comment
Share on other sites

From here:

 

{"results":[{"text":"David Mecklenburger","value":"desplaines11"},{"text":"Gary Geppert","value":"desplaines11"},{"text":"David Chapa","value":"desplaines11"},{"text":"Kyle Fischer","value":"desplaines11"}]}

 

I need David Mecklenburger, Gary Geppert, David Chapa and Kyle Fischer to post to the database and process page - not "desplaines11".

Link to comment
Share on other sites

You're right, but that means you need to select something other than `team_names_id` as value. If they don't have a dedicated id field I suppose you could just select `team_member` again?

As in SELECT DISTINCT team_member as text, team_member as value FROM `team_members` WHERE team_names_id='%s'

Link to comment
Share on other sites

Thanks for helping. 

 

It's doing the same thing for locations and team names drop down box too. 

 

I have three tables, locations, team members and team names.  Team names relates to locations, and team members relates to team names. 

 

Seems like it's a simple solution somewhere - I just can't figure it out!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.