Delete data without refreshing the page

Asked

Viewed 828 times

2

I am no longer using extra pages to use functions inside the PHP page. That is PDO use with a php for functions and use everything on the page without loading anything external.

My doubt is how to use ajax in this case.
I want to send a button from a form, but the function is inside the page.
I quote the code.

This is an example to delete data:

Form sent to this snippet

if (isset($_POST['deletepost'])){

   if(empty($errors) === true){
      $postiddel = $postid;

      $users->deletepost($postiddel);
      header('Location: home');
      exit();
   }
}

HTML

<form method="post" name="deleteform" action="">

    <input type="text" name="postid" hidden value="<?php echo $postid; ?>">
    <li><button type="submit" name="deletepost" class="dropdownbutton">Eliminar publicação</button></li>

</form>

What I need to do, is run php that is inside the same page without refreshing the page.... I do not know if this is possible, but I hope it is, because I am tired of using external pages. inserir a descrição da imagem aqui

  • Have you read this question/answer? http://answall.com/a/6634/129

  • I’ve been looking at most of the posts on stackoverflow, but even this isn’t quite what I’m looking for... Because that’s not quite what I’m looking for, I’m looking for an ajax function that sends the form without loading the page... I’m going to add some inlustrations to help

  • What I wanted was something like this but without an external page: http://stackoverflow.com/questions/12614167/delete-record-without-refresh-page-using-php

  • 2

    PHP is never "inside the page". It mounts the page, and sends the result to the client. When the client sees the assembled page, PHP has already done its part (at least in the part the client has already received). The most you’ll get is to run the same PHP again, or another mechanism, through a call by the client (either via JS, form sending, or Websockets).

1 answer

5

Using the Submit button using HTML will always call whatever is in the action and reload the page. An alternative method is to loop in javascript all input fields (inputs, selects, etc) through some selector, for example a jquery selector like $(':input) and pass the data through ajax. Jquery example:

function enviarDados() {
    $.ajax({
        type: POST, url: 'http://localhost/aplicacao',
        data: $(':input')
    }).done( function( res ) {
        alert('dados enviados');
    })
} 

vc can place the call to this function on the Submit button without it sending the page that way:

<input type='submit' value='Enviar' onclick='enviarDados(); return false;'>

'Return false' on onclick prevents the button from making the form’s actual Submit. So you can call an ajax function that sends the data without the form being sent.

  • 3

    With jQuery not even need to loop, has a method serialize that you apply in the form and get the values of all fields.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.