0

I've a such PHP-script:

<?php
 $menuItemList = getSubPkgCategForDDList(echo "<script>showSubCatForMenuItem();</script>");
 if(isset($menuItemList)){
 foreach($menuItemList as $u){
 ?>
 <p><span contenteditable="true"><?php echo $u->name ?></span><button type="button" class="btn btn-danger btn-xs" onclick="deleteCategory(<?php echo $u->pkg_cat_ddlist_id ?>)">Delete</button>
 <button type="button" class="btn btn-success btn-xs" onclick="editCategory(<?php echo $u->pkg_cat_ddlist_id ?>,<?php echo "'".$u->name."'" ?>)">Save</button></p>
 <?php
 }
 }
?>

Function getSubPkgCategForDDList must generate html-code,so it depends from parameter, which is send to this function. I get this parameter from such js-function showSubCatForMenuItem():

function showSubCatForMenuItem(){
 console.log($('#menuItem').val());
 return $('#menuItem').val();
 }

This function takes data from such dropdown list:

<select id="menuItem" onchange="showSubCatForMenuItem()">
 <?php
 $itemList = getPackCategoriesForAsideMenu();
 if(isset($itemList)){
 foreach($itemList as $u){
 ?>
 <option value="<?php echo $u->pkg_cat_ddlist_id ?>"><?php echo $u->name ?></option>
 <?php
 }
 }
 ?>
 </select>

How to do that parameter transfer is correctly, when I load page and select item from dropdown list? Sorry for my English.

asked Oct 19, 2015 at 11:41
2
  • 1
    you need to call ajax request on dropdown change to send variables to php script. Commented Oct 19, 2015 at 11:43
  • 1
    Either use AJAX or send the value to the same as GET, POST Commented Oct 19, 2015 at 11:46

2 Answers 2

1

You should take advantage of $_SESSION in this case. Now print out the drop down :

<select id="menuItem">
<?php
 $itemList = getPackCategoriesForAsideMenu();
 if(isset($itemList)){
 foreach($itemList as $u){
 echo'<option value="'.$u->pkg_cat_ddlist_id.'">'.$u->name.'</option>';
 }
 }
?>
</select>

Write the JS script :

$("#menuItem").live('change',function(){
 var val = $(this).val();
 $.post('change.php',{data:val},function(){
 // Do some 
 });
});

And create a php file named change.php :

<?php
session_start();
if(!empty($_POST['data'])){
 $_SESSION['menu_sltd'] = (int) $_POST['data']; // It makes sure that the data sent is integer / number
}
?>

Now, change your main script to :

<?php
session_start();
$menu_sltd = (!empty($_SESSION['menu_sltd']) ? $_SESSION['menu_sltd'] : 'default id'); // Default id is the default menu id if it's blank
$menuItemList = getSubPkgCategForDDList($_SESSION['menu_sltd']);
if(isset($menuItemList)){
 foreach($menuItemList as $u){
 echo'
 <p>
 <span contenteditable="true">'.$u->name.'</span>
 <button type="button" class="btn btn-danger btn-xs" onclick="deleteCategory('.$u->pkg_cat_ddlist_id.')">Delete</button>
 <button type="button" class="btn btn-success btn-xs" onclick="editCategory('.$u->pkg_cat_ddlist_id.',\''.$u->name.'\')">Save</button>
 </p>';
 }
}
?>

GOOD LUCK,, glad to help you. Don't give up

answered Oct 19, 2015 at 12:17
Sign up to request clarification or add additional context in comments.

2 Comments

may be, I'm not expert in PHP, but to my mind, using SESSION isn't very good idea. I thought many time, and accepted an ajax method instead of it, at last. But may be, your method deserves respect too, I can't be sure about it, because while I'm not expert in PHP. And I vote up and accept your answer to do it best for your work. Thanks!
Thanks, someday you'll know the use of SESSION
0

In javascript function showSubCatForMenuItem() you can set the value of selected item in some hidden field on every change event the value selected by user will get updated, then while saving the use this value that is saved in the hidden field.

Niranjan N Raju
12k4 gold badges24 silver badges41 bronze badges
answered Oct 19, 2015 at 11:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.