Boy Pasmo
Boy Pasmo

Reputation: 8471

click button execute php file then redirect

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

Upvotes: 1

Views: 1746

Answers (2)

Steve Robbins
Steve Robbins

Reputation: 13812

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");

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

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.

Upvotes: 2

Related Questions