0

I am new to stackoverflow and am after a little help. I have already tried searching for what I am about to ask, but cannot find any relevant topics, so here goes:

I have a PHP page, that gets a list of venues and it's town/city from a table within a MySQL database, which I have then concatenated to populate a dropdown, e.g. each <option> will display "[venue], [town/city]".

What I am trying to do is when the user selects one of the options, I want to store the [venue] and [town/city] as separate fields in another table within the MySQL database.

I would really appreciate any help.

sgspragg
  • 19
  • 6

2 Answers2

1

More details might be needed. For instance do you need to know how to insert records on database? or are you also having trouble getting values of option tag?

But from what I understood:

First remember to set value attribute of option tags:

<option value="venue,town">venue,town</option>

Then after submitting the form, you can slice the returned string. Assume you stored "venue,town" inside a variable named $str

$results = explode(",",$str);

$results will be an array with two elements. $results[0] contains "venue" and $results[1] contains "town"

I am not sure how much it helped but you can give more details and I'll get back

sinaza
  • 820
  • 6
  • 19
  • Thanks for the response. I'm fine with the database side of things, it's purely for retrieving values on the PHP side. Explode is an option, but what I did omit from my question is that the venue may contain commas, sorry. Is there any way of getting the last value after the comma for the town/city and retrieving the remaining for the venue? Hope this explains a bit better – sgspragg Feb 07 '15 at 13:38
  • Then I guess this might help http://stackoverflow.com/questions/16610791/explode-only-by-last-delimeter – sinaza Feb 07 '15 at 13:51
0

I think the best way is to put the datarecord ids (primary key value) into the value attribute of the <option> tags, then on the server side requery the data of the selected id from the database. This way your form can't be manipulated with unwanted data.

To be more clear, I would do it like in this simplified semi pseudocode:

<?php
$action = isset($_GET['action']) ? $_GET['action'] : null;

if($action == "save") {
  $id = $_POST['venue'];

  if(!empty($id)) {
    /* fetch data belonging to $id from database and then save venue and town/city
       as separate fields in another table */
  }
}

/* fetch all data for the selectbox from the db and store it in $data */
?>
<form action="?action=save" method="post">
  <select name="venue" size="1">
    /* iterate through $data and create $id and $caption */
    <option value="<?php echo $id; ?>"><?php echo $caption; ?></option>
    /* iteration end */
  </select>

  <input type="submit" value="save" />
</form>
Mario A
  • 3,286
  • 1
  • 17
  • 22
  • This answer is again a really good option as I already use the venue ID to retrieve the list of venues and it's associated town/city. This is another option for me to look at. Thank you – sgspragg Feb 07 '15 at 13:42