2

I have the following function that will show a pop up message, once the user click ok it will navigate the user to the controller called backendBanner and call the function delete_banner.

The link written in html and php looks something like this:

<a href="<?php echo base_url().'backendBanner/delete_banner/'.$banner['banner_path']; ?>">

Code for the button in view to call the function

<td>
      <a onclick="return remove_image($(this));" rel="<?php echo $banner['banner_path']; ?>"><input type="button" class="btn btn-danger" value="Delete"></a>
</td>

Code for the function In this function, $banner['banner_path'] is equal to fullpath

<script>
        function remove_image(img)
        {
            if(confirm('<?php echo lang('confirm_remove_image');?>'))
            {
                var fullpath  = img.attr('rel');
                alert(fullpath);
                redirect(base_url()."backendBanner/delete_banner/"+fullpath);
            }
        }
    </script>

How should I go about bringing the user to the link through the function?

6
  • 1
    how are you currently calling the remove_image() function? Commented Jan 22, 2016 at 14:42
  • 1
    It should be window.location="<?php echo base_url().'backendBanner/delete_banner/'.$banner['banner_path']; ?>" + fullpath Commented Jan 22, 2016 at 14:42
  • Always use a console when you're working with clientside code, it will tell you if you're using an undefined function. Commented Jan 22, 2016 at 14:47
  • you are vulnerable to JS injection attacks. don't echo out arbitrary text from PHP into a JS context. if your translation of confirm_remove_image contains ANY quote characters you'll be introducing a JS syntax error and killing the entire <script> block. always output text into JS via json_encode(). Commented Jan 22, 2016 at 14:49
  • @VickyGonsalves Thanks for your help. It works well. Commented Jan 22, 2016 at 15:22

3 Answers 3

1

It should be :

window.location="<?php echo base_url().'backendBanner/delete_banner/'.$banner['banner_path']; ?>" + fullpath
Sign up to request clarification or add additional context in comments.

6 Comments

Can u also help me with another problem that I am facing? The value of fullpath is dynamic it can be in the following format "assets/images/banner3.jpg" and "banner4.jpg". The problem I am facing is that, when the fullpath is "assets/images/banner3.jpg" and I var_dump the value of $image_name in the delete_banner controller, it only returns "assets" instead of "assets/images/banner3.jpg".
try var_dump($banner) and check ?
the value is pass over through "fullpath"
Can you create php fiddle?
When i var_dump(fullpath) it is "assets/images/banner3.jpg"
|
1

The problem in your JS code is that you're using redirect(). This will not work because redirect() is a PHP/Codeigniter function, not JS.

Use window.location instead.

Comments

1

Assuming that the function base_url() it's a valid function created by you...

Try with:

location.href = base_url() + img.attr('rel');

Note: Try to use some templating engine like twig, blade etc.

Comments

Your Answer

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.