简体   繁体   中英

Remove values from array onclick without page refresh

I have a one form and onclick i want to remove image id from array without page refresh and after submit form i will update it in the DB.

I am using following code for this but no any success.

PHP Code:-

$total = count($oldimage);
                    //print_r($oldimage);
                    for($i=0;$i<$total;$i++)
                    {
                        $fimgsql="select * from cc_tbl_img_vid_upload where id='$oldimage[$i]'";
                        $gimgname=mysqli_query($db,$fimgsql) or die('Error');
                        $rw=mysqli_fetch_assoc($gimgname);
                        ?><b id="rm<?=$i?>">Remove<?=$rw['upload_url']?></b><br/><br/>
                            <script>$( "#rm<?=$i?>" ).click(function() {
                                <?php unset($oldimage[$i]);?>
                                alert(<?php echo $oldimage[$i]; ?>);
                                $( "#rm<?=$i?>" ).hide();
                            });</script>
                        <?php
                    }
                        sort($oldimage);
                        $oldimage=implode(',',$oldimage);

Unfortunately you can't directly execute PHP (server-side) based on a JavaScript (client-side) event like you have in your code.

You need to handle your arrays in JavaScript. You can json_encode a PHP array and use the JSON array in your JavaScript, which is probably the closest you will get to doing what you're trying to do.

It's not possible the way you want to do it.PHP is serverside - which means you need to send a request to the server and get the response back(refreshing/changing page) while js is client side, and manipulates the UI, in order to achive that you must use AJAX (making a request from behind, and not seen by the user and getting the response, and based on it's output change the UI for the user)

Read this link - http://api.jquery.com/jquery.ajax/ ajax isn't that hard, but you must understand it, if you have php/mysql/html/css/js/jquery knowledge you will get it in no time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM