1,105,242 Community Members

Dynamically Manage Radio Button PHP JS

Member Avatar
diafol
Where are my eyes?
12,963 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

I just need to save the data on page after REFRESH and save in DB.

Yes I know, that's why I discussed your options. ANyway you need to save BEFORE refresh.

Any example of CODE would be great.

I've already said:

I'm afraid I don't have the time to create an update script.

It's your job to do that. If you get stuck with it, by all means post a question.

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

It is not working AFTER refresh anymore:

// php
<div style="padding-left:280px;">
                            <!--  Add -->
                            <input type="button" class="btnAjouterConfigure" id="add" value="Ajouter" style="text-decoration:none; width:100px; height:41px; line-height:34px; font-weight:bold;" onclick="addnature()"/>    
                            &nbsp;
                            <!--  Delete -->
                            <input type="button" class="btnSupprimerConfigure" id="delete" value="Supprimer" style="text-decoration:none; width:100px; height:41px; line-height:34px; font-weight:bold;" onSubmit="return deleteRadioButton();">
                            <div id="fooBar"></div> 
                        </div>
                        <br />

                        <div style="height:auto; padding-left:280px;" id="mainRadio">
                        <?php if(mysql_num_rows($rs)){ ?>
                            <?php while($row = mysql_fetch_assoc($rs)){ ?>
                            <div id="<?php echo $row['libelle']?>_r" class="marginTop">
                                <span><input type="radio" name="<?php echo $row['libelle']?>" id="<?php echo $row['libelle']?>"></span> 
                                <span id="<?php echo $row['libelle']?>_label"><?php echo $row['libelle']?></span>
                            </div>
                            <?php }?>
                        <?php }?>

                        </div>
<script type="text/javascript">
jQuery(function(){
    jQuery('#add').click(function(){

        if(jQuery('#xxx_add_e').length){
            return false;
        }else{
            //var name = jQuery.trim(jQuery('#radio').val());

            var elem = '<input type="radio" name="" id="xxx_add_e" />';
            var row = '<div id="xxx_add_r"><span>'+elem+'</span><span id="xxx_add_label"><input type="text" value="" placeholder="enter value" id="xxx_add_t" /></span> <span id="xxx_add_btns"><span><input type="button" value="okay" id="xxx_add_y" onclick="xaddElement();" /></span> <span><input type="button" value="cancel" id="xxx_add_n" onclick="xcancelElement();" /></span></span></div>';
            jQuery('#mainRadio').prepend(row);

        }

    });
    jQuery('#delete').click(function(){

        for(var i=0; i<jQuery(':radio').length; i++){
            var rad = jQuery(':radio')[i];
            if(jQuery(rad).is(':checked')){
                //make an ajax call here to delete record from database
                jQuery.ajax({
                    url:'radio.php?delete=1&name='+jQuery(rad).attr('name'),
                    success: function(){

                    },
                    complete: function(){
                        jQuery('#loading').hide();
                    }
                });
                jQuery('#'+jQuery(rad).attr('id')+'_r').remove();
            }
        }
    });

});
function xaddElement(){
        var val = jQuery('#xxx_add_t').val();

        jQuery('#loading').show();
        ////Make an ajax call to add element data in database
        jQuery.ajax({
            url:'radio.php?add=1&name='+val,
            success: function(html){

            },
            complete: function(){
                jQuery('#loading').hide();
            }
        });

        jQuery('#xxx_add_btns').remove();
        jQuery('#xxx_add_label').text(val);
        jQuery('#xxx_add_label').attr('id',val+'_label');
        jQuery('#xxx_add_r').attr('class','marginTop');
        jQuery('#xxx_add_r').attr('id',val+'_r');
        jQuery('#xxx_add_e').attr('name',val);
        jQuery('#xxx_add_e').attr('id',val);
}
function xcancelElement(){
    jQuery('#xxx_add_r').remove();
}

</script>

// radio.php
<?php
if($_REQUEST['add'] == 1){
    $name = trim($_REQUEST['name']);
    $sql = "insert into user_nature set ref_user=1, libelle='$name'";
    mysql_query($sql);
}
if($_REQUEST['delete'] == 1){
    $name = trim($_REQUEST['name']);
    $sql = "delete from user_nature where libelle='$name'";
    mysql_query($sql);
}

?>
Member Avatar
diafol
Where are my eyes?
12,963 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Ok, this seems to be js/jQuery as opposed to php - how about moving it to that forum?

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

I did. I hope i will get the expected result.

Member Avatar
diafol
Where are my eyes?
12,963 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

I did. I hope i will get the expected result.

?? this is still in the php forum.

Member Avatar
PriteshP23
Posting Whiz in Training
206 posts since Oct 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 6 [?]
 
0
 

Finally,i got it.. :)

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    jQuery(function(){
        jQuery('#add').click(function(){

            if(jQuery('#xxx_add_e').length){
                return false;           
            }

            else{
                //var name = jQuery.trim(jQuery('#radio').val());

                var elem = '<input type="radio" name="" id="xxx_add_e" />';
                var row = '<div id="xxx_add_r"><span>'+elem+'</span><span id="xxx_add_label"><input type="text" placeholder="enter value" id="xxx_add_t"/></span> <span id="xxx_add_btns"><span><input type="button" value="OK" id="xxx_add_y" onclick="xaddElement();" /></span> <span><input type="button" value="Cancel" id="xxx_add_n" onclick="xcancelElement();" /></span></span></div>';

                jQuery('#mainRadio').prepend(row);
            }

        } );
        jQuery('#delete').click(function(){

            for(var i=0; i<jQuery(':radio').length; i++){
                var rad = jQuery(':radio')[i];
                if(jQuery(rad).is(':checked')){
                    //make an ajax call here to delete record from database
                    jQuery.ajax({

                        //url:'radio.php?action=delete&name='+jQuery(rad).attr('name'),
                        url:'radio.php?action=delete&name='+jQuery(rad).val(),
                        success: function(){
                            window.location = window.location.href;
                        },
                        complete: function(){
                            jQuery('#loading').hide();
                        }
                    });
                    jQuery('#'+jQuery(rad).attr('id')+'_r').remove();
                }
            }
        });

    });
    function xaddElement(){
            var val = jQuery('#xxx_add_t').val();
            //alert(val);
            jQuery('#loading').show();
            ////Make an ajax call to add element data in database 
            var myurl = 'radio.php?action=add&name='+val;
            //alert(myurl);
            jQuery.ajax({
                  url:myurl,

                success: function(html){
    //alert(html);
                },
                complete: function(){
                    jQuery('#loading').hide();
                },
                onError:function(xhr){
                alert(html);
                }
            });

            jQuery('#xxx_add_btns').remove();
            jQuery('#xxx_add_label').text(val);
            jQuery('#xxx_add_label').attr('id',val+'_label');
            jQuery('#xxx_add_r').attr('class','marginTop');
            jQuery('#xxx_add_r').attr('id',val+'_r');
            jQuery('#xxx_add_e').attr('name',val);
            jQuery('#xxx_add_e').attr('id',val);
    }
    function xcancelElement(){
        jQuery('#xxx_add_r').remove();
    }

    </script>
    <div id="loading" style="position: fixed;width:100%;top:0;left:0;right:0;bottom:0;background:#f7f7f7;opacity:0.4;display:none;">
        <div id="" style="text-align:center;width:100%;position:relative;top:100px;color:#fff">
        Loading...
        </div>
    </div>
Question Answered as of 1 Year Ago by diafol and LastMitch
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: