0

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.

14
  • 1
    Don't mix PHP and HTML code, please... Commented Oct 16, 2014 at 11:24
  • 1
    Please what? @TomaszKowalczyk I am trying to construct html dynamically via PHP. Commented Oct 16, 2014 at 11:28
  • 1
    Just replace your hyperlink (that's presumably styled to resemble a button) with an actual submit button! Commented Oct 16, 2014 at 11:29
  • 2
    $('.appDeatilsButton').on('click', function(event) { .. btw you miss-spelled details Commented Oct 16, 2014 at 11:32
  • 1
    @satheeshwaran Then compute necessary data in PHP and then pass them to the proper view renderer. Don't do that in one place, it is a Separation of Concerns violation and something that will definitely bite you in the long run. Commented Oct 16, 2014 at 11:33

1 Answer 1

1

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.

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

1 Comment

I tried it but getting error, Cannot read property 'submit' of undefined.

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.