0
votes

So I have an HTML page (has some PHP on it loading a functions.php and a header.php page) and upon post, it executes a function that is located on functions.php page successfully. I am trying to return a value back to the html page after post that would indicate a success or error but I need it to be displayed within a certain div, otherwise I would just let the functions.php page echo the result. I'd prefer not to use ajax but if I have to then I will.

EDIT: Sorry, forgot to explain what is happening. Duh. So after post, it shows both the success and the error message, as if both if statements are being returned at the same time. It will show "Updated Successfully!" AND "Error!" at the same time.

Page 1 (html page):

include('session.php');
include('header.php'); // Includes Header Script
include('functions.php');

$cus_details=getAddress($login_id);

foreach($cus_details as $cus_details) {
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   $result = updateAddress($login_id, $_POST);
}
?>
<body style="background-image: url('Scripts/background.png'); -moz-background-size: cover; -webkit-background-size: cover; -o-background-size: cover; background-size: cover;">
<center><div id="signup">
<div class="container1">
<form class="addressForm" action="" method="post">
<?php if ( $result == true ) { ?>
    <br><br><center><span4>Updated Successfully!</span4></center>
<? } else if ( $result == false ) { ?>
    <br><br><center><span4>Error!</span4></center>
<?php } ?>
..... (more html)

Page 2 (functions page):

// Update Customer Address
function updateAddress($login_id, $data) {
    // Establishing Connection with Server by passing server_name, user_id and password as a parameter
    $connection = mysqli_connect("localhost", "root", "");
    // Selecting Database
    $db = mysqli_select_db($connection,"db_name");
    // SQL Query To Fetch Complete Information Of User
    $ses_sql=("update rma_customer_address set cus_address_1='".$data['address1']."' WHERE cus_id='$login_id' AND cus_address_type=0");
    if (mysqli_query($connection, $ses_sql)) {
        $success = true;
    } else {
        $success = false;
    }
    mysqli_close($connection);
    return $success;
}
2
Why would you have to use AJAX? Do you understand the difference between it and what you're doing now? How is it failing? Does your server support short tags, because you're using them (<? instead of <?php) in the middle of your if/elseif which could be causing your issues... - William_Wilson
What isn't working with your current code? - Chris Wheeler
What happens if you just have <?}else { ?> <br><br><center><span4>Error!</span4></center> <?php } ?> - Isaac
@William_Wilson welp, no it does not support short tags and yep, it was me using <? instead of <?php for the else if statement... Just got to work and still haven't had my second cup of coffee. My apologies and thank you. - Marz_89
You have a major sql vulnerability in your code by injecting data directly from $_POST into your SQL query. - domdambrogia

2 Answers

0
votes

It looks like the issue here is you are closing out your PHP before actually running the else elseif code. Leaving you with standard HTML to output both your Success and Error message statements. I would try running those both in your PHP and using the echo command.

<?php if ( $result == true ) { 
       echo "<br><br><center><span4>Updated Successfully!</span4></center>";
      }
      else if ( $result == false ) {
        echo "<br><br><center><span4>Error!</span4></center>";
      }
?>

Given the foreach loop functions properly.

0
votes

I am posting @William_Wilson's comment as the answer as it was user error on my part which he saw. Does your server support short tags, because you're using them <? instead of <?php in the middle of your if/elseif which could be causing your issues