1

Good day to all,

Been searching all day on how to do this. What I want is to make every 'click' to go into one php file and determine what will be the action.

itemAction.php

    include 'inc/database.php';
    include 'inc/functions.php';

    if ($_GET['action'] == 'delete') {
        // do delete action <-- this one is working
    } else if ($_GET['action'] == 'edit') {
        // do edit action; NOW HERE I WANT TO REDIRECT TO ANOTHER PAGE
        header("location: edit.php"); // I can't do something like this. Why?
    }

html

<div class="action">    
    <a id="delete" href="itemAction.php" rel="<!--some_id-->"><img src="images/trash.ico" alt="delete"></a>
    <a id="edit" href="itemAction.php" rel="<!--some_id-->"><img src="images/Pencil-icon.png" alt="edit"></a>
</div>

js

$("div.action a#delete").click(function (e) {
    var decision = confirm("Are you sure you want to delete the item?");
    if (decision) {
        e.preventDefault();
        $.get('itemAction.php', {action : 'delete', id : $(this).attr("rel")}, function (data) {            
            location.reload();
            alert("Succssfully deleted!");
        });
    }
    return false;
});

$("div.action a#edit").click(function (e) {
    e.preventDefault();
    $.get('itemAction.php', {action : 'edit', id : $(this).attr("rel")});
});

The delete action seems to be working.. But I can't do I want in the edit action which is to redirect to other page. What better ways are there to do it that way? Any help would be much appreciated. Thanks

2 Answers 2

2

You can not do this because ajax will only send you response html, text, xml or json response but can not do redirection.

For redirecting you must return anything say "redirectme." and based on that response you need to add code in javascript to redirect at desired location.

what you can do is?

in php file add below code,

echo json_encode(array('status' => 'edit', 'url' => 'edit.php'));

based on above response modify your $.get response callback as below.

$.get('itemAction.php', {action : 'delete', id : $(this).attr("rel")},
   function (data)
   {
     if(response.status == 'edit'){
       window.location = response.url;
     }
});

it just a guide line you need set it according to your need.

Comment Response

if js is disabled then you need to code accordingly.

first of all you need to modify your html links as below,

<a id="delete" href="itemAction.php?action=delete&id=someid"
<a id="edit" href="itemAction.php?action=edit&id=someid"

and by clicking on above link use their href attribute to pass into $.get as below.

$.get( $(this).href()

by doing so it js is disabled your code will work too.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I can do something like window.location.href=somelink; right? My concern is.. what if the user disables the javascript. Well, thanks again.
@BoyPasmo Yes that is the concern but if js disabled then your delete will also not work right..??
Yes and that goes to all of my page functionalities because most of it are javascript built in javascript. Thanks for a great response.
0

Have your button go somewhere

<form method="get" action="someScript.php">
    <button>Do things</button>
</form>

Then in someScript.php

<?php

// do things

header("Location: redirectToHere.php");

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.