0

I'm creating a site with multiple sections, each with multiple categories, and need to pass 2 values through the form to add articles to my table. Here's an example of what I'm trying to do with my Form + PHP file:

<select name="sec-cat">
<option value-1="0">Select One</option>
<option value-1="0">---</option>
<option value-1="music" value-2="news">News Updates</option>
<option value-1="music" value-2="features">Features</option>
<option value-1="music" value-2="articles">Articles</option>
<option value-1="0" value-2="0">---</option>
<option value-1="lifestyle" value-2="news">News Updates</option>
<option value-1="lifestyle" value-2="features">Features</option>
<option value-1="lifestyle" value-2="articles">Articles</option>
etc

I know that value-1 & value-2 aren't valid, but hopefully it will give an idea of wht I'm trying to do

<?php
$section = mysqli_real_escape_string($con, $_POST['sec-cat']); // Value-1
$category = mysqli_real_escape_string($con, $_POST['sec-cat']); // Value-2

if($section === '0'){
echo "No Section Specified";
exit();
}
?>

Is there a solution for extracting values 1 & 2 using a simple PHP method?

Wayne Six
  • 11
  • 6
  • 1
    Possible duplicate of [Can an Option in a Select tag carry multiple values?](http://stackoverflow.com/questions/3245967/can-an-option-in-a-select-tag-carry-multiple-values) – CBroe May 15 '17 at 09:31
  • Saw this for the JSON solution but missed the php answer beneath. I'll give the `explode` method a try – Wayne Six May 15 '17 at 09:37

1 Answers1

1

I can see one way of doing it: Add extra data to your post, separated by a known delimiter. So, your input field would change from this:

<option value-1="music" value-2="news">News Updates</option>

Into this:

<select name="sec-cat">
<option value="music_news" >News Updates</option>

And on the server you can get the values like this:

$catRetVal = explode ('_',$POST['sec-cat']);
echo $catRetVal[0];
echo $catRetVal[1];

Is this what you are trying to do?

Curious Mind
  • 659
  • 10
  • 26
  • It is as same as http://stackoverflow.com/a/16229998/2679536. and this is a better solution than others. – Shaunak Shukla May 15 '17 at 09:43
  • Yes - possible duplicate. – Curious Mind May 15 '17 at 09:44
  • 1
    Yes, this is exactly what I'm trying to do. I'll give this a try now and see what the results are. Thanks Pedro! – Wayne Six May 15 '17 at 09:44
  • If it works, please accept the answer - it would be my first. – Curious Mind May 15 '17 at 09:47
  • 1
    I edited the code to `$seccat = mysqli_real_escape_string($con, $_POST['sec-cat']); $seccat_explode = explode ('_',$seccat); $section = $seccat_explode[0]; $category = $seccat_explode[1];`, for use when adding to a table - But this works perfectly. Thanks again. – Wayne Six May 15 '17 at 10:00