last day (14 days later) » 

10:06 AM
0
Q: passing multiple option values in php

The Naga TankerI'm trying to assign multiple value to option in php so i can extract them later and separately and insert to mysql where is how i'm trying to assign. <select name="val[parent]"> <?php if($cnt > 0){ while($parent = mysqli_fetch_assoc($sql)) { ?> <option value="<?php echo $parent['category_id']....

 
Presumably you just need to explode the $Value = explode("-",$_POST['val']['parent']), then assign $CategoryID = $Value[0] & $TradeID = $Value[1] ?
 
let me try that
nopes not working.
 
Try list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2); It's a bit difficult to troubleshoot without seeing your full form. Try print_r($_POST), to see what you are actually posting to your PHP.
 
i've added the full form.
this is what i get with the 'print_r' Array ( [val] => Array ( [parent] => 1-ER [category_name] => dsads ) )
 
@TheNagaTanker check my answer, hopefully it will help.
0
A: passing multiple option values in php

JustBaronFrom your form, you can assign your PHP variables like so: if(isset($_POST['val']['parent'])){ # split the value by "-", and assign to category_id & trade_id list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2); } if(isset($_POST['val']['category_name'])){ # set t...

 
10:10 AM
hi
 
Alright there mate?
Have you tried my answer?
 
yes
i'm trying it right now
on your answer where can i fetch the trade id
trade_id
 
It should be assigned here: list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);
 
I've just updated my answer to include your $parent variable.
So if the value of $_POST['val']['parent'] = "1-ABC"
$parent = "1-ABC";
$category_id = "1";
$trade_id = "ABC";
If it works for you, and you find it helpful, please accept (tick) my answer and upvote.
 
10:24 AM
i want to extract the trade_id seperately too
Array ( [val] => Array ( [parent] => 2-WD [category_name] => kllkjkj ) )
 
Your trade_id is "WD", correct?
 
yes
there is how it was exploded
this is* <option value="<?php echo $parent['category_id']. "-" . $parent['trade_id'];?> "> <?php echo $parent['trade_name']; ?> </option>
 
Then this will work:
list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);
echo "category_id: $category_id<br/>"; // 2
echo "trade_id: $trade_id"; // "WD"
 
thanks
that echoes fine
but i have to assign it to a variable so i can insert it to the sql
$trade_id = $trade_id;
 
You already have it assigned ($trade_id), that's how you can now echo it out.
$trade_id = $trade_id; will do nothing
 
10:40 AM
okay
 
And what's the point of $category_id, if you are not stroing it in the DB?
*storing
And as previous, if it works for you, and you find it helpful, please accept (tick) my answer and upvote.
 
Sure ill do
 
10:57 AM
.....
@TheNagaTanker Problems?
Do you know how to accept answer?
You have a few questions, that users have spent time in attempting to answer, to help you. It benefits everyone when the OP (you) accept and upvote answers.
FYI ;)
 
ill do that sure
what i realized is the values are not being passed on
Array ( [val] => Array ( [parent] => 2 WD [category_name] => dawdwadawdaw ) ) category_id: 0 trade_id:
the cat and trade_id are null
print_r($_POST);
echo "category_id: $category_id ";
echo "trade_id: $trade_id";
 
Your $_POST['val']['parent'] variable doesn't have a hyphen "-", which means your explode function won't work correctly.
Before it was "2-WD" now it is "2 WD".
Your explode function `explode("-", ....)` will work for the fist.
The second will result in:
`$category_id = "2 WD";`
 
11:25 AM
thanks --
now it wont insert to DB
ummm
 
11:39 AM
thank you so much for you help. Here is the final code -- eveything works as expected
if(isset($_POST['val']))
{
list($category_id, $trade_id) = explode("-", $_POST['val']['parent'], 2);
$aVals = $_POST['val'];
$category_id = $category_id;
$category_name = $aVals['category_name'];
$parent = $category_id;
if(isset($aVals['category_id']))
{
$category_id = $aVals['category_id'];
}
$sql = "INSERT INTO category SET category_name = '$category_name', parent = $parent, vcat_id = '$trade_id', status = 1";
$sql = mysqli_query($databaseLink,$sql);
header('location:category.php');

}
 
Good, glad to help
`$category_id = $category_id; ` is pointless.
You are still not using the `$category_id` in the query.
 

last day (14 days later) »