1

I have to make everything happened on the same page. I have used action="<?PHP echo $_SERVER['PHP_SELF']; ?>" here but it is not working. I have insert the PHP query below the form. Basically, my question is how do I make sure the form is posting the values on the same page. If it is updated, a pop up will come up.

$user_id=$_SESSION['user_id'];
$date = date("l jS \of F Y h:i:s A");

$query1 ="SELECT daily_limit FROM user WHERE user_id='$user_id'"; 
$result1 = mysqli_query($link, $query1) or die(mysqli_error($link));
while ($row1 = mysqli_fetch_array($result1)) {
    $dailylimit=$row1['daily_limit'];
}               

$query2 = "SELECT SUM(debit) AS debited_today FROM transaction WHERE user_id = '$user_id' AND date = CURRENT_DATE" ;
$result2 = mysqli_query($link, $query2) or die (msqli_error($link));
while ($row2 = mysqli_fetch_array($result2)){
    $debited_today = $row2['debited_today'];
}

// form

    <form method="POST" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" > 
        <table id="table">
            <tr>
                <td class="alt">Existing Daily Limit</td> 
                <td>S$ <?php echo $dailylimit; ?> </td>
                <input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
            </tr> 
            <tr>
                <td class="alt"><label for="newdailylimit">New Daily Limit</label></td>
                <td>$ <select name="newdailylimit">
                        <option value="100.00">100.00</option>
                        <option value="500.00">500.00</option>
                        <option value="1000.00">1000.00</option>
                        <option value=5000.00">5000.00</option>
                    </select></td>
            </tr>
            <tr>
                <td class="alt">Amount Debited Today</td>
                <td>S$ <?php echo $debited_today; ?></td>
            </tr>
            <tr>
                <td class="alt">Amount Debited Left</td>
                <td>S$ <?php echo ($dailylimit - $debited_today); ?> </td>
            </tr>
        </table>
        <br/>
        <input type="submit" name="submit "value="Submit"></input>
    </form>

// Values I need to POST

$dailylimit = $_POST['dailylimit'];
$newdailylimit = $_POST['newdailylimit'];

if ($dailylimit != $newdailylimit){
    $query = "UPDATE user SET daily_limit='$newdailylimit' WHERE user_id='$user_id'";
    $result = mysqli_query($link, $query) or die(mysqli_error($link));
    echo "<script>alert('You have successfully updated your daily limit');</script>";
}
else if ($dailylimit == $newdailylimit){
    echo "<script>alert('You have selected the same daily limit as your previous one. Please choose a different one. ');</script>";
}
else{

}
user3210617
  • 67
  • 2
  • 3
  • 7

2 Answers2

1

Ahh I may have found out what the problem is:

You have a space after the word submit and no space before value.

This will prevent your form from submitting.

<input type="submit" name="submit "value="Submit"></input>
                              ---^ ^

Change this to:

<input type="submit" name="submit" value="Submit">

The double quote might mess things up.


Other things that are wrong but won't fix your problem

Also

<option value=5000.00">5000.00</option>

should be

<option value="5000.00">5000.00</option>

Also

<input type="hidden" name="dailylimit" value="<?php echo $dailylimit ?> "/>
                                       // You have an extra space here ^  

Which will change your $dailylimit, and append it with a space.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • 1
    Having the `` in `` won't make a difference, but the `name="submit "` with the space like that "will". – Funk Forty Niner Jan 22 '14 at 18:14
  • 1
    Plus the space in `value=" "/>` doesn't matter. Just so you know. I `+1` you btw. Because of the actual (most probable) cause being the submit button issue. `name="submit "` ;-) --- Good catch btw. – Funk Forty Niner Jan 22 '14 at 18:20
  • @Fred-ii- Thanks! Yeah the last two suggestions won't fix his problem, but not fixing them may cause other problems later down the line. – Arian Faurtosh Jan 22 '14 at 18:25
  • 1
    You're welcome. Well, personally I think it's the submit button thing as you noted. I tried/tested with the OP's original code, and it didn't submit. Then when I noticed your answer, I knew that THAT was the most likely cause, I changed it to that, and it worked. – Funk Forty Niner Jan 22 '14 at 18:28
-2

make your action=''. it will post to itself. then on top of your page check if the request is post ex. if($_POST){//add your code}else{//yourform}

Shahin Khani
  • 199
  • 4
  • 14