2

So I've been spending hours on finding something specific to my issue, but I can't seem to find any.

I'm in the middle of creating a small CMS. My problem is that I don't know how to make a submitted form in a UI dialog to do the action of PHP_SELF inside of the UI dialog. I have a list of user which can be selected by a radio button. There is a delete button which has some javascript attached:

$('#delete_user').on('click', function(e) {
    if (id != null ) {
        var url = "delete_user.php?id=" + id;
        $('#dialog-placeholder').dialog().load(url);
        $('.ui-dialog :button').blur();
    }
    e.preventDefault();
    return false;
});

Now, my problem is that I have made it to the UI dialog where i get the ID sent with the url, but I have no idea how to send a form and still keep it in the dialog with the underneath PHP:

    <?php 
if ((isset($_GET['id'])) && is_numeric($_GET['id'])) {
    $id = $_GET['id'];
} elseif ((isset($_POST['id'])) && is_numeric($_POST['id'])) {
    $id = $_POST['id'];
} else {
    echo "<p>An error has occurred. Please try again.</p>";
    echo "<a href=\"#\" class=\"close_dialog\">Close</a>";
$jQuery_close = <<<msg
<script>
    $('.close_dialog').on('click', function(){
        location.reload();
        dialog("close");
    });
</script>
msg;

echo $jQuery_close;
    exit();
}
require('includes/db_con.php'); //making a connection to the database
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['sure'] == 'Yes') {
        $q = "DELETE FROM users WHERE user_id=$id LIMIT 1";
        $r = @mysqli_query($dbc, $q);

        if (mysqli_affected_rows($dbc) == 1) {
            echo "<p>The user has been deleted</p>";
        } else {
            echo "<p>The user could not be deleted due to system error. Please try again.</p>";
        }
    } else {
        echo "The user has NOT been deleted!";
    }
} else {
    $q = "SELECT email, CONCAT(firstname, ' ',lastname) AS name FROM users WHERE user_id='$id'";
    $r = @mysqli_query($dbc, $q);
    $num = mysqli_num_rows($r);

    if ($num = 1) {
        while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
            echo "<p>Are you sure you want to delete this user?</p>";
            echo $row['email'] . '<br />';
            echo $row['name'];
        }

        echo '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">
        <input type="HIDDEN" name="id" value="' . $id .'" />
        <input type="submit" name="sure" value="Yes" />
        <input type="submit" name="sure" value="No" />
        </form>';   
    } else {
        echo "This page has been accessed in error";
    }
} 
mysqli_close($dbc);
?>

When pressing "yes" or "no" in the form, it just directly goes to a new page.

My question is, how do I fire the php and send the form within the dialog?

Nelson
  • 49,283
  • 8
  • 68
  • 81
vnJ
  • 45
  • 3
  • Just looking to submit form with JQuery? Dupe? http://stackoverflow.com/questions/1200266/submit-a-form-using-jquery – ficuscr Nov 06 '13 at 20:06
  • Hi ficuscr, yes it is somewhat what I'm looking. I just don't understand how it fires the php when using jQuery to submit. But I will give it a try. Thank you – vnJ Nov 06 '13 at 20:23

1 Answers1

2

Place your e.preventDefault() at the start of the delete click handler, not at the end where you currently have it, it should be like this:

$('#delete_user').on('click', function(e) {
    e.preventDefault();
    if (id != null ) {
        var url = "delete_user.php?id=" + id;
        $('#dialog-placeholder').dialog().load(url);
        $('.ui-dialog :button').blur();
    }
    return false;
 });
Nelson
  • 49,283
  • 8
  • 68
  • 81
  • You're welcome, as you seem new to stackoverflow, remember to accept answer (green mark) if the answer solved your problem. – Nelson Nov 06 '13 at 20:17
  • I'm quite new, but I know that the (green mark) is when the answer solved the problem. But it didn't. It did correct a mistake in structure though. What I'm looking for is more of the direction ficuscr pointed me. – vnJ Nov 06 '13 at 20:39
  • Ohh and I would like to have giving you some points, but I just don't have enough. – vnJ Nov 06 '13 at 20:47