Satheesh
Satheesh

Reputation: 11276

Redirect to a PHP file on a HTML button click with some data being sent via POST

I want to redirect to a PHP file when a button on my initial screen is pressed.

$stmt->bind_result($app_id, $user_id, $app_name, $app_desc,$api_key);
while ($stmt->fetch()) {
    echo '<form class="appFragmentForm" id="'.$app_id.'" action="appDetails.php" method="POST">
                    <div class="col-sm-6 col-md-3"><div class="thumbnail">
                               <img class="img-rounded appIcon" src="img/icon_app_placeholder.png">
                  <div class="caption text-center">
                    <h3>'.$app_name.'</h3>
                    <p>'.$app_desc.'</p>
                    <p><a href="#" class="btn btn-danger appDeatilsButton" id="'.$app_id.'" name="appDeleteButton" role="button">Delete App</a> <a href="#" class="btn btn-default appDeatilsButton" id="'.$app_id.'" role="button">View Details</a></p>
                  </div>
                </div>
              </div>
              </form>'; 
}
}

Javascript function:

$(".appDeatilsButton").click(function (event) { 
    $.ajax({
  type: "POST",
  url: "appDetails.php",
  data: { ID: this.id}
 })
  .done(function( msg ) {
       window.location(appDetails.php);
   });
 });

appDetails.php

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();     
$appID = $_POST["appID"];  
echo '<script language="javascript">alert('.$appID.')</script>';    
?>

I know I am completely wrong, please help me in doing this in a eaiser way. FYI I am a mobile developer with little insight on web programming so web experts please excuse my newbie language.

Upvotes: 0

Views: 269

Answers (1)

Cliff Burton
Cliff Burton

Reputation: 3744

Put this.form.submit(); instead of window.location(appDetails.php); in your JavaScript event handler (your javascript function).
And check errors in your code.
in the first code you posted on 9th line you give appDeatilsButton (maybe appDetailsButton...I don't know if it is a mis-spell) class to the "Delete" button.. the event handler $(".appDeatilsButton").click() runs the script whenever an element with class appDeatilsButton is clicked and it submits the form even if "Delete" button is clicked.

Upvotes: 1

Related Questions