Okay, this is my first time posting here, so if I'm doing something wrong please let me know. Here's my question:

I've got a page full of pets (this is for an online game). The client wants to be able to select multiple pets at once and change the group that the pet is in. Currently, it's set up to work on a per pet basis. There's a field in the database for the pet's group and all already set up. I know this should be a simple fix but I'm pulling a blank. I've got the javascript select all and the checkboxes all set up - it's just the action behind it. I cannot seem to remember how to actually have the function work for ALL that are selected. Any help would be appreciated. I'm not asking for you guys to write it for me - just point me in the right direction on where to go. I'm assuming I'd use a loop, I just can't seem to figure out exactly how.

Recommended Answers

All 5 Replies

Hi

I'm having trouble understanding what you need.

Can you post your html and JavaScript for us to see?

I guess reading your post that you already have an action selecting “a pet” and change its group. How do you do this , by a radio button and a submit ? If so you just have to select one more field in each pet that is checkbox and a different submit.

Okay, let me explain a little better. Here is a screenshot for reference: Here is the link to the photo. In the photo provided, each pet is listed in a box. Above the box is a select dropdown menu that gives care/movement options to group your pets. This dropdown is on an individual basis - a per pet basis. Above that, there is a select checkbox. That checkbox, if checked, allows to select multiple pets at once. My goal is to allow a user to select more than 1 pet and move them all at the same time or say put them all up for sale at the same time. This is our current script. And as a note, it's based upon the SMF forum software and has been modified by my clients old programmer. I'm being hired to do additional features and bug fixes so the current code is not mine.

function template_view_pets(){
    global $context, $settings, $scripturl, $txt;

    if(array_key_exists('u', $_REQUEST) && is_numeric($_REQUEST['u'])){
        $user_id = $_REQUEST['u'];
    }else{
        $user_id = $context['user']['id'];
    }

    $current_pets = explode(',', $context['user']['current_pets']);
    $sort = (array_key_exists('sort', $_REQUEST)) ? $_REQUEST['sort'] : false;

    function pets_alphabetical_sort($a, $b){
        if (strtolower($a['name']) == strtolower($b['name'])) {
            return 0;
        }elseif(strtolower($a['name']) > strtolower($b['name'])){
            return 1;
        }else{
            return -1;
        }
    }
    $alphabetical = '';
    if('alphabetical' == $sort){
        usort($context['user']['pets'], "pets_alphabetical_sort");
        $alphabetical = ' selected="selected"';
    }

    $pets_in_room = array();
    foreach($context['user']['all_pets'] as $pet){
        if(!array_key_exists($pet['room'], $pets_in_room)){
            $pets_in_room[$pet['room']] = array();
        }
        if(0 == $pet['is_orphan']){
            $pets_in_room[$pet['room']][] = $pet['id'];
        }
    }

    $room = (array_key_exists('room', $_REQUEST)) && is_numeric($_REQUEST['room']) ? $_REQUEST['room'] : '1';
    $user = (array_key_exists('u', $_REQUEST)) && is_numeric($_REQUEST['u']) ? $_REQUEST['u'] : $context['user']['id'];
    $page = (array_key_exists('page', $_REQUEST) && is_numeric($_REQUEST['page'])) ? $_REQUEST['page'] : false;
    $pets_per_page = 20;

    if(!array_key_exists($room, $pets_in_room)){
        $max_pages = 1;
    }else{
        $max_pages = ceil(count($pets_in_room[$room])/$pets_per_page);
    }
    $page = ($page > $max_pages) ? $max_pages : $page;

    if($page > $max_pages){
        $page = $max_pages;
    }elseif($page <= 0){
        $page = 1;
    }
    $previous_page = ($page-1 <= 1) ? 1 : $page-1;
    $next_page = ($page+1 >= $max_pages) ? $max_pages : $page+1;
    $start_pet = $page ? ($page-1)*$pets_per_page : 1;

    if('' == $context['user']['pets'] || '0' == $context['user']['pets']){
        echo '<div class="windowbg" style="text-align: center">You don\'t have any pets yet. Click <a href="/index.php?action=pets&sa=pet_shop">here</a> to adopt one!</div>';
        die;
    }

    /*echo '<select id="change_room_select" onchange="navigate_to_room()">
        <option value="none">'.$txt['pets_room_to_view'].'</option>';
    foreach($context['user']['pets_user_settings']['rooms'] as $existing_room => $bool){
        $selected = ($room == $existing_room) ? ' selected="selected" ' : '';
        echo '<option value="'.$existing_room.'"'.$selected.'>'.$existing_room.'</option>';
    }
    echo '</select><br/>';*/

    echo 'Sort by: <select id="current_pet_sort" onchange="change_sort()">
            <option value="adoption">Adoption Date</option>
            <option value="alphabetical"'.$alphabetical.'>Alphabetical Order</option>
        </select><br/>';
    echo 'Page: <span id="first_pet_page"><a href="/index.php?action=pets&sa=view_pets&u='.$user_id.'&page=1&room='.$room.'">&#8592;&#8592;</a></span>
    <span id="previous_pet_page"><a href="/index.php?action=pets&sa=view_pets&u='.$user_id.'&page='.$previous_page.'&room='.$room.'">&#8592;</a></span>
    <span id=""><input id="current_pet_page" type="text" value="'.$page.'"/></span>
    <span id="next_pet_page"><a href="/index.php?action=pets&sa=view_pets&u='.$user_id.'&page='.$next_page.'&room='.$room.'">&#8594;</a></span>
    <span id="last_pet_page"><a href="/index.php?action=pets&sa=view_pets&u='.$user_id.'&page='.$max_pages.'&room='.$room.'">&#8594;&#8594;</a></span>';

    echo '<div id="pet_rooms">';
    foreach($context['pets_rooms'] as $room_id => $room_name){
        $pets_shown = 0;
        $pets_to_show_in_room = 10;
        $current_pet = ($room == $room_id ? ' current_pet' : '');
        if(!array_key_exists($room_id, $pets_in_room)){
            $pets_in_room[$room_id] = array();
        }
        echo '<div class="pet_room'.$current_pet.'" onclick="window.location = \''.$scripturl.'?action=pets&sa=view_pets&room='.$room_id.'&u='.$user_id.'\'">
                <div id="pet_room_name_'.$room_id.'" class="pet_room_name" title="'.$txt['pets_room_name_edit'].'">'.censorText($room_name).'</div>
                <div class="pets_in_room">';
        foreach($pets_in_room[$room_id] as $pet){
            $pet = $context['user']['pets'][$pet];
            if($pets_shown >= $pets_to_show_in_room){
                echo '<div class="pets_room_img_container pets_not_shown" title="More pets not shown">...</div>';
                break;
            }
            echo '<div class="pets_room_img_container"><img src="'.$settings['default_images_url'].'/pets/owned/mini-'.$pet['id'].'.png"/></div>';
            $pets_shown++;
        }
        echo '</div>
            </div>';
    }

    if((array_key_exists('u', $_REQUEST) && $_REQUEST['u'] == $context['user']['id']) || $context['user']['is_admin']){
        echo '<div id="add_pet_room" class="pet_room" onclick="add_pet_room()"><img src="'.$settings['default_images_url'].'/plus_sign.png"/></div>';
    }

    echo '</div>';

    if ($room != 1 && ($context['user']['is_admin'] || $user == $context['user']['id']))
        echo '<div style="margin-top: 10px; margin-bottom: 10px;"><a href="', $scripturl, '?action=pets&sa=delete_pet_room&u=', $user, '&room=', $room, '" onclick="return confirm(\'', $txt["delete_room_warning"], '\');">', $txt['delete_this_room'], '</a></div>';
    echo'   <script language="JavaScript">
                function toggle(source) {
                  checkboxes = document.getElementsByName(\'group\');
                  for(var i=0, n=checkboxes.length;i<n;i++) {
                    checkboxes[i].checked = source.checked;
                  }
                }
        </script>

        <input type="checkbox" onClick="toggle(this)" /> Select All (All on this page)<br/>';
        if($context['user']['is_admin'] || $context['user']['id'] == $pet['user_id']){
            echo '<select id="action_select_'.$pet['id'].'" class="action_select" onchange="action_controller('.$pet['id'].')">
                <option value="none">--Select Action--</option>
                <option value="give_away">'.$txt["give_away_pet"].'</option>
                <option value="give_to_orphanage">'.$txt["give_to_orphanage"].'</option>
                <option value="take_leave_pet">'.$txt['take_leave_pet'].'</option>
                <option value="get_stats">'.$txt['pets_get_stats'].'</option>
                <option value="change_room">'.$txt['pets_change_room'].'</option>
                ', empty($pet['is_sale']) ? '<option value="put_sale">'.$txt['pets_put_sale'].'</option>' : '<option value="put_off_sale">'.$txt['pets_put_off_sale'].'</option>', '
            </select> With Selected Pets';
        }

    if (!empty($pets_in_room[$room]))
        $pets_to_show = array_slice($pets_in_room[$room], $start_pet, $pets_per_page);
    else
        $pets_to_show = array();
//  echo '<pre>';
    //print_r($context['user']['pets']);
//  echo '</pre>';
    $i = 1 ;
    foreach($pets_to_show as $pet){
        $pet = $context['user']['pets'][$pet];
        if($room !== $pet['room']){
            continue;
        }
        if($i%5 == 1){
            echo '<div class="pet_row">';
        }
        /*This must also be set in javascript*/
        $max_pets = 20;
        $change_type = 'add';
        $current_pet = '';
        if(in_array($pet['id'], $current_pets)){
            $change_type = 'remove';
            $change_type = 'remove';
            $current_pet = ' current_pet';
        }
        if (!empty($pet['is_sale']))
            $current_pet .= ' sale_pet';

        $change_type_onclick = '';

        if($change_type == 'add' && count($current_pets) >= $max_pets){
            $change_type_href = '';
            $change_type_onclick = 'onclick="alert(\'You may have only '.$max_pets.' pets.\')"';
        }else{
            $change_type_onclick = 'onclick="change_bring_pet('.$pet['id'].')"';
        }
        if(0 == $context['user']['orphans']){
            $orphan_pet_onclick = 'onclick="orphan_pet_dialog('.$pet['id'].')"';
        }else{
            $orphan_pet_onclick = 'onclick="alert(\'You already have a pet in the orphanage\')"';
        }

        echo '
        <div id="pet_container_'.$pet['id'].'" class="pet_own_col">';
        if($context['user']['is_admin'] || $context['user']['id'] == $pet['user_id']){
            echo '<input type="checkbox" name="group"><select id="action_select_'.$pet['id'].'" class="action_select" onchange="action_controller('.$pet['id'].')">
                <option value="none">--Select Action--</option>
                <option value="give_away">'.$txt["give_away_pet"].'</option>
                <option value="give_to_orphanage">'.$txt["give_to_orphanage"].'</option>
                <option value="take_leave_pet">'.$txt['take_leave_pet'].'</option>
                <option value="clickable_link">'.$txt['pets_clickable_link'].'</option>
                <option value="get_stats">'.$txt['pets_get_stats'].'</option>
                <option value="change_room">'.$txt['pets_change_room'].'</option>
                ', empty($pet['is_sale']) ? '<option value="put_sale">'.$txt['pets_put_sale'].'</option>' : '<option value="put_off_sale">'.$txt['pets_put_off_sale'].'</option>', '
            </select>';
        }
        echo'
            <div id="pet_button_'.$pet['id'].'" class="pet_button user_pet'.$current_pet.'">
                <img class="gender_view_pets" src="'.$settings['default_images_url'].'/'.$pet['gender'].'-pet.png"/>';
        if($context['user']['is_admin'] || $context['user']['id'] == $pet['user_id']){
            echo'<span id="pet_name_'.$pet['id'].'">'.$pet['name'].'</span><span class="edit_name edit_button" onclick="update_name('.$pet['id'].')"></span><br/>';
        }else{
            echo'<span id="pet_name_'.$pet['id'].'">'.$pet['name'].'</span><span class="edit_name" onclick="update_name('.$pet['id'].')"></span><br/>';
        }
        if (!empty($pet['is_sale']))
            echo '
                        <img src="', $settings['default_images_url'], '/poster/sendmoney.png" style="width: 16px; vertical-align: bottom;" /> <span>', round($pet['sale_price'], 2), '</span><br />';

        echo '<a href="/index.php?action=pets&sa=show_pet&pet_id='.$pet['id'].'">
                        <img class="full_pet_image" src="'.$settings['default_images_url'].$pet['image'].'?'.$pet['growth_stage'].'" title="'.$txt['pet_take_with'].' ('.$pet['posts_with'].' posts)"/>';
        echo '
                    </a><br/>';
        echo'
            </div>
            <span id="take_leave_pet_info_'.$pet['id'].'" class="take_leave_pet_info" style="display: none">'.$txt['take_leave_pet_info'].'</span>';
        if($context['user']['is_admin'] || $context['user']['id'] == $pet['user_id']){
            $day_of_secs = 24*60*60;
            $clicks_info = json_decode($pet['clicks_info'], true);
            $benefit_bar_width = 200;
            $growth_info = json_decode($pet['growth_info'], true);
            if(array_key_exists($pet['growth_stage']+1, $growth_info)){
                $posts_with  = $pet['posts_with'];
                if(1 > $posts_with){
                    $posts_with = 1;
                }
                if(!array_key_exists('clicks_info', $pet)){
                    $clicks_info['total_clicks'] = 1;
                }
                $days_off_from_post = $posts_with/$growth_info[$pet['growth_stage']]['posts_to_shorten'];
                $days_off_from_clicks = $clicks_info['total_clicks']/$growth_info[$pet['growth_stage']]['clicks_to_shorten'];
                $percent_to_max_benefit = ($days_off_from_post+$days_off_from_clicks)/($growth_info[$pet['growth_stage']]['max_growth_time'] - $growth_info[$pet['growth_stage']]['min_growth_time']);
                //$percent_to_max_benefit = $posts_with/(($growth_info[$pet['growth_stage']]['max_growth_time'] - $growth_info[$pet['growth_stage']]['min_growth_time'])*$growth_info[$pet['growth_stage']]['posts_to_shorten']);
                if($percent_to_max_benefit > 1){
                    $percent_to_max_benefit = 1;
                }
                $days_until_growth =round(((strtotime('+'.$growth_info[$pet['growth_stage']]['max_growth_time'].' days', $pet['adopted'])-((floor($posts_with/$growth_info[$pet['growth_stage']]['posts_to_shorten'])*$day_of_secs)+floor($clicks_info['total_clicks']/$growth_info[$pet['growth_stage']]['clicks_to_shorten'])*$day_of_secs))-time())/$day_of_secs, 1);
                if((time()+($days_until_growth*$day_of_secs)) < ($pet['adopted'] + ($growth_info[$pet['growth_stage']]['min_growth_time']*$day_of_secs))){
                    $days_until_growth = round((($pet['adopted']+($growth_info[$pet['growth_stage']]['min_growth_time']*$day_of_secs))-time())/$day_of_secs, 1);
                }
                if($days_until_growth < 0){
                    $days_until_growth = 0;
                }
            }else{
                $percent_to_max_benefit = false;
                $days_until_growth = false;
            }
            if(!is_numeric($clicks_info['total_clicks'])){
                $clicks_info['total_clicks'] = 0;
            }
            if(!is_numeric($pet['posts_with'])){
                $pet['posts_with'] = 0;
            }

            //#percent_to_max_benefit inline styles to make it simple to change width of benefit bar.
            echo'<div id="pet_stats_'.$pet['id'].'" class="stats_div">';
            if(false === $percent_to_max_benefit){
                echo '<div>'.$txt['pets_fully_grown'].'</div>';
            }
            echo '<div>'.$txt['pets_date_adopted'].': '.date('l F jS, Y', $pet['adopted']).'</div>';
            if(false !== $percent_to_max_benefit){
                echo $txt['pets_percent_max_benefit'].'<div class="percent_to_max_benefit" style="width: '.$benefit_bar_width.'px"><span class="max_benefit_bar" style="height: 20px; width: '.$benefit_bar_width*$percent_to_max_benefit.'px; display: inline-block; background-color: #00FF00"></span></div>';
            }
            echo '<div>'.$txt['pets_posts_with'].': '.$pet['posts_with'].'</div>';
            echo '<div>'.$txt['pets_total_clicks'].': '.$clicks_info['total_clicks'].'</div>';
            if(false !== $percent_to_max_benefit){
                echo '<div>'.$txt['pets_days_until_growth'].': '.abs($days_until_growth).'</div>';
            }
            echo '<div>'.$txt['pets_personality'].': '.ucfirst($pet['nature']).'</div>';
            echo '</div>';
        }
        echo '<div id="clickable_link_dialog_'.$pet['id'].'" class="clickable_info">
                '.$txt['pet_clickable_text'].'<hr/><span class="actual_link">
                [url='.$scripturl.'?action=pets&sa=click_clickable&clickable='.$pet['id'].'][img]'.$settings['default_images_url'].$pet['image'].'[/img][/url]
            </div>
        </div>';
        if($i%5 == 0){
            echo '</div>';
        }
        $i++;
    }
    echo '
    <div id="give_away_dialog" style="display: none"><input id="give_away_user" type="text" placeholder="Enter user id of recipient"/><input id="give_away_pet" type="hidden"/><input onclick="submit_give_away()" type="button" value="'.$txt["give_away_pet"].'"/></div>
    <div id="orphan_pet_dialog" style="display: none"><input id="orphan_pet" type="hidden"/><div>'.$txt["give_to_orphanage_warning"].'</div><input onclick="submit_orphan_pet()" type="button" value="'.$txt["give_to_orphanage"].'"/></div>
    <div id="change_room_dialog" style="display: none"><input id="room_dialog" type="hidden"/>
    <select id="change_pet_room_id" onchange="change_room()">
        <option value="none">'.$txt['pets_change_room_select'].'</option>"';
    foreach($context['pets_info']['user']['rooms'] as $room_id => $room_name){
        echo '<option value="'.$room_id.'">'.$room_name.'</option>';
    }
    echo '</select>
    </div>
    <div id="put_sale_dialog" style="display: none">
        <input id="price_pet" type="text" placeholder="Enter price for the pet"/>
        <input id="put_sale_pet" type="hidden"/>
        <input onclick="submit_put_sale()" type="button" value="'.$txt["pets_put_sale"].'"/><br />Pets on sale appear first in the room, if you can\'t find it please see on the first page
        </div>
    <div id="add_room_dialog" style="display: none">
        <input id="add_room_name" name="add_room_name" type="text" placeholder="New Room Name"/>
        <input type="button" onclick="submit_add_pet_room()" value="Create"/>
    </div>
    <div id="error_div" style="display: none"></div>
    <script type="text/javascript">
        $(".pet_room_name").on("click", function(event){
            switch_to_input($(this).attr("id"), "room_name");
            event.stopPropagation();
        });
        $("#change_pet_room_id").keyup(function () {
            this.value = this.value.replace(/[^1-9\.]/g,"");
        });
        $("#current_pet_page").keypress(function(e){
            var room = "'.$room.'";
            if(e.which == 13) {
                var page = $("#current_pet_page").val();
                window.location = "/index.php?action=pets&sa=view_pets&u='.$user_id.'&page="+page+"&room="+room;
            }
        });
        function navigate_to_room(){
            var selected_room = $("#change_room_select").val();
            window.location = "/index.php?action=pets&sa=view_pets&u='.$user_id.'&room="+selected_room;
        }
        function change_sort(){
            var page = $("#current_pet_page").val();
            var sort = $("#current_pet_sort").val();
            var room = "'.$room.'";
            window.location = "/index.php?action=pets&sa=view_pets&u='.$user_id.'&page="+page+"&sort="+sort+"&room="+room;
        }
        function action_controller(pet_id){
            switch($("#action_select_"+pet_id).val()){
                case "give_away":
                    give_pet_dialog(pet_id);
                    break;
                case "give_to_orphanage":
                    orphan_pet_dialog(pet_id);
                    break;
                case "take_leave_pet":
                    change_bring_pet(pet_id);
                    break;
                case "clickable_link":
                    get_clickable_dialog(pet_id);
                    break;
                case "get_stats":
                    show_stats(pet_id);
                    break;
                case "change_room":
                    show_change_room(pet_id);
                    break;
                case "put_sale":
                    put_sale(pet_id);
                    break;
                case "put_off_sale":
                    put_off_sale(pet_id);
                    break;
            }
            $("#action_select_"+pet_id).val("none");
        }
        function put_sale(pet_id){
            $("#put_sale_pet").val(pet_id);
            $("#put_sale_dialog").dialog({
                modal: true
            });
        }
        function put_off_sale(pet_id) {
            var script_url = "'.$scripturl.'";
            $.get(script_url + "?action=pets&sa=pet_sale&pet_id=" + pet_id, function() {
                location.reload();
            });
        }
        function submit_put_sale(){
            var price = $("#price_pet").val();
            var pet_id =  $("#put_sale_pet").val();
            var script_url = "'.$scripturl.'";
            $.get(script_url + "?action=pets&sa=pet_sale&pet_id=" + pet_id + "&price=" + price, function() {
                location.reload();
            });
        }
        function show_change_room(pet_id){
            change_room_pet_id = pet_id;
            $("#change_room_dialog").dialog({
                modal: true
            });
        }
        function change_room(){
            var room = $("#change_pet_room_id").val();
            var script_url = "'.$scripturl.'";
            $.get("'.$scripturl.'?action=pets&sa=change_pet_room&pet_id="+change_room_pet_id+"&u='.$user_id.'&room="+room+"&xml", function(data){
                if("success" == data.trim()){
                    window.location.href = script_url+"?action=pets&sa=view_pets&room='.$room.'&u='.$user_id.'";
                    /*$("#change_room_dialog").dialog("close");
                    $("#pet_container_"+change_room_pet_id).fadeOut();*/
                }else{
                    alert("Something went wrong, please try again. If you continue to get this error, please alert staff.");
                }
            });
        }
        function show_stats(pet_id){
            $("#pet_stats_"+pet_id).dialog({
                modal: true,
                width: \'auto\',
                height: \'auto\'
            });
        }
        function get_clickable_dialog(pet_id){
            $("#clickable_link_dialog_"+pet_id).dialog({
                modal: true
            });
        }
        function give_pet_dialog(pet_id){
            $("#give_away_pet").val(pet_id);
            $("#give_away_dialog").dialog({
                modal: true
            });
        }
        function orphan_pet_dialog(pet_id){
            $("#orphan_pet").val(pet_id);
            $("#orphan_pet_dialog").dialog({
                modal: true
            });
        }
        function change_bring_pet(pet_id){
            $.get("'.$scripturl.'?action=pets&sa=change_bring_pet&pet_id="+pet_id+"&u='.$user_id.'&xml", function(data){
                var response = $.parseXML(data);
                var result = $(data).find("result");
                if("added" == result.text()){
                    $("#pet_button_"+pet_id).addClass("current_pet");
                }else if("removed" == result.text()){
                    $("#pet_button_"+pet_id).removeClass("current_pet");
                }
                $("#take_leave_pet_info_"+pet_id).fadeIn();
                setTimeout(function(){$("#take_leave_pet_info_"+pet_id).fadeOut()}, 5000);
            });
        }
        function submit_give_away(){
            var user_id = $("#give_away_user").val();
            var pet_id =  $("#give_away_pet").val();
            var script_url = "'.$scripturl.'";
            if($("#pet_button_"+pet_id).hasClass("current_pet")){
                change_bring_pet(pet_id).done(function(){
                    window.location = script_url+"?action=pets&sa=give_pet&user_id="+user_id+"&pet_id="+pet_id+"&u='.$user_id.'";

                });
            }else{
                window.location = script_url+"?action=pets&sa=give_pet&user_id="+user_id+"&pet_id="+pet_id+"&u='.$user_id.'";
            }
        }
        function submit_orphan_pet(){
            var pet_id = $("#orphan_pet").val();
            var script_url = "'.$scripturl.'";
            window.location = script_url+"?action=pets&sa=orphan_pet&pet_id="+pet_id+"&u='.$user_id.'";
        }

        function update_name(pet_id){
            switch_to_input("pet_name_"+pet_id, "name");
        }
        function update_description(pet_id){
            switch_to_input("pet_description_"+pet_id, "description");
        }
        function submit_name_update(input_id){
            var new_name = $("#"+input_id).text();
            var pet_id = input_id.replace(/[^0-9\.]/g, "");
            var script_url = "'.$scripturl.'";
            $.get(script_url+"?action=pets&sa=change_name&change_name="+new_name+"&pet_id="+pet_id+"&xml", function( data ) {
            });

        }
        function submit_description_update(input_id){
            var new_description = $("#"+input_id).text();
            var pet_id = input_id.replace(/[^0-9\.]/g, "");
            var script_url = "'.$scripturl.'";
            $.get(script_url+"?action=pets&sa=change_description&change_description="+new_description+"&pet_id="+pet_id+"&u='.$user_id.'&xml", function( data ) {
            });

        }
        function switch_to_input(input_id, submit_type) {
            var input = "<input id=\""+input_id+"\" value=\""+$("#"+input_id).text()+"\" type=\"text\">";
            $("#"+input_id).replaceWith(input);
            $("#"+input_id).on("blur", function(){switch_to_span(input_id, submit_type)});
            $("#"+input_id).on("keypress", function(e){
                if(13 == e.which){
                    switch_to_span(input_id, submit_type);
                }
            });
            $("#"+input_id).select();
        };
        function switch_to_span(input_id, submit_type) {
            var span = "<span id=\""+input_id+"\">"+$("#"+input_id).val()+"</span>";
            $("#"+input_id).replaceWith(span);
            if("name" == submit_type){
                submit_name_update(input_id);
            }else if("description" == submit_type){
                submit_description_update(input_id);
            }else if("room_name" == submit_type){
                submit_pet_room_name_change(input_id);
            }
        };

        function add_pet_room(){
            $("#add_room_dialog").dialog({
                "modal": true
            });
        }
        function submit_add_pet_room(){
            var script_url = "'.$scripturl.'";
            var add_room_name = $("#add_room_name").val();
            if("" != add_room_name){
                $.get(script_url+"?action=pets&sa=add_pet_room&add_room_name="+add_room_name+"&u='.$user_id.'&xml", function(data) {
                    var parsed_data = JSON.parse(data);
                    if(parsed_data.status != "success"){
                        alert("An error occurred, please try again later. If this keeps happening, alert staff.");
                    }else{
                        window.location.href = script_url+"?action=pets&sa=view_pets&room='.$room.'&u='.$user_id.'";
                    }
                });
            }
        }

        function submit_pet_room_name_change(input_id){
            var script_url = "'.$scripturl.'";
            var new_room_name = encodeURIComponent($("#"+input_id).text());
            var room_id = input_id.replace(/\D/g,\'\');
            $.get(script_url+"?action=pets&sa=rename_pet_room&new_room_name="+new_room_name+"&room_id="+room_id+"&u='.$user_id.'&xml", function(data) {
                var parsed_data = JSON.parse(data);
                if(parsed_data.status != "success"){
                    alert("An error occurred, please try again later. If this keeps happening, alert staff.");
                }else{
                    window.location.href = script_url+"?action=pets&sa=view_pets&room='.$room.'&u='.$user_id.'";
                }
            });
        }
    </script>
    <br style="clear: both"/>';
}

Any help would be greatly appreciated!

Member Avatar for diafol

That is one hell of a block of code. You've got a mishmash of php, markup and js. Very hard to understand. Is all of it relevant? Not many contributors are going to wde through 500+ lines. Maybe cut it down to the relevant stuff. Also tidy up the php/markup - they don't need to be mixed up.

Maybe what you should do is to understand the codes by yourselves and following @diafol's suggestion to cut it down to the relevant stuff.
As from what I viewed, you simply copied and paste the code from your 'clients old programmer' and expect we do the job for you. Maybe you should tell us what you have tried so far and give detail problem on what you faced that prevent you to complete it by your own?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.