0

When i enter new values to form input field and submit them, then twice page reload needed to get the new values.

like you can see the $balance_2 in the 7th line. This line sum all row values of every row and and record the value in Balance column of every row and after pressing submit button then it goes to update_2.php file to update the database and when in click on

<a style='left: -18%; top: 100%; position: absolute; color: white; font-family: Roboto, helvetica, arial, sans-serif;  width: 170px; font-weight: 600;' href='index_2.php'>Click here to go back</a>

button in update_2.php file and go back to the main page then the form values are not updated till i reload it again.

I want that when i click on Click here to go back button then new values should be shown and there must not be need to reload the page twice.

This is my codes

$id_2 = $row['ID'];
$Budget_2 = $row['Budget'];
$Availed_in_Regions_2 = $row['Availed_in_Regions'];
$Requested_in_KBL_2 = $row['Requested_in_KBL'];
$Received_in_KBL_2 = $row['Received_in_KBL'];
$Availed_in_KBL_2 = $row['Availed_in_KBL'];

$balance_2 = $Availed_in_Regions_2 + $Requested_in_KBL_2 + $Received_in_KBL_2 + $Availed_in_KBL_2;
$con2->query("UPDATE Office_Operations f1, (SELECT SUM(balance) AS bal FROM Office_Operations ) f2 SET ytotal6_2 = bal WHERE f1.id = 1;");
$con2->query("UPDATE Office_Operations SET Balance = $balance_2 WHERE id = $id_2");


echo "<div class='calc_container'"; if($row['ID']==1) echo " style='margin-bottom:40px;'"; echo "> 

    <input type='hidden' class='id_3' name='id[]' value='".$row['ID']."'>

    <input type='text' class='budget_3' name='Budget[]' value='".$row['Budget']."'>

    <input type='text' class='avail_region_3' name='Availed_in_Regions[]' value='".$row['Availed_in_Regions']."'>

    <input type='text' class='req_kbl_3' name='Requested_in_KBL[]' value='".$row['Requested_in_KBL']."'>

    <input type='text' class='rec_kbl_3' name='Received_in_KBL[]' value='".$row['Received_in_KBL']."'>

    <input type='text' class='avail_kbl_3' name='Availed_in_KBL[]' value='".$row['Availed_in_KBL']."'>

    <input type='text' class='balance_3' name='Balance[]' value='".$row['Balance']."'>

    </div>";}
Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73
ARA
  • 83
  • 10
  • What does your update_2.php file look like? And where does the anchor tag for 'Click to go back' live? – SteveK Apr 08 '15 at 05:24
  • This is [update_2.php](http://jsfiddle.net/9tnjhdrx/) and Click to go back lives in update_2.php. – ARA Apr 08 '15 at 05:31

2 Answers2

0

From my perspective:

  1. Put all the input tags inside of form tag that uses action="" (to the same URL)

OR

  1. Process the request with AJAX. When clicked on anchor a use onclick then define the listener function that makes the AJAX call and updates the input fieldS on successful response (parse the response accordingly). See using JS or jQuery

Also, it would be better if you escape the query input values. For MySQL see some posts here, here and here.

Community
  • 1
  • 1
sitilge
  • 3,687
  • 4
  • 30
  • 56
  • See the `input` code snippet here: http://codepad.org/gK6icyD3 . What I did was just wrapped the inputs inside of form that send the request to the current URL in _POST variable when the button (newly created) is pushed. – sitilge Apr 08 '15 at 06:49
  • i replaced it but nothing have changed – ARA Apr 08 '15 at 09:34
0

Your list page or your main page is the index_2.php and your update_2.php is where the Click here to go back button is located.

Summary:

index_2.php

  • List of data
  • Also where the form is

update_2.php

  • where the Click here to go back button is located

When the data is submitted from your index_2.php, it will go to update_2.php, but does nothing but offers only the back button.

The only time the UPDATE query will run is when the user clicks the Click here to go back button.

SOLUTION:

  • Put your UPDATE query in your update_2.php
  • Use the header() function to redirect the user back to index_2.php after the query

Sample Code:

index_2.php:

<form action="update_2.php" method="POST">

  <!-- INSERT HERE YOUR INPUT FIELDS -->
  <input type="submit" name="submit">
</form>

update_2.php:

<?php

  if(isset($_POST["submit"])){

    /* INSERT HERE YOUR UPDATE QUERIES */
    header("LOCATION:index_2.php"); /* REDIRECT USER BACK TO index_2.php */

  } /* END OF ISSET SUBMIT */

?>
Logan Wayne
  • 6,001
  • 16
  • 31
  • 49
  • Sorry for misunderstanding This is [update_2.php](http://jsfiddle.net/9tnjhdrx/) and Click to go back lives in update_2.php. – ARA Apr 08 '15 at 09:32