0

I have a simple form before:

<form method="post" action="firstbillsubmitbal.php?id=#COL1#"> 
    <input name="currentbal" type="text" id="currentbal" class="input-mini"  /> 
    <input type="submit" id="submit" value="Save" class="btn btn-success" />
</form>

Which calls this page for processing firstbillsubmitbal.php

$dealID = $_GET["id"]; 
$currentbal = mysql_real_escape_string(stripslashes($_POST['currentbal']));
$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID'") or die (mysql_error());

It was working fine for single transactions. But I need to edit it a bit since I am displaying my data in a table now. I now have this js code when I click on a btn on my table per row, passes my row_id and currentbal calue, I got it working but I would like to know from this js code how do I process my form?

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
    var r=confirm("Are You Sure You Want To Proceed?");
    if(r==true) {
        alert("Record is saved");
    } else {
        alert("Cancelling Transaction");
    }   
}

The js code has two variables only at the moment which is

row_id = this is basically the ID of the db row and currentbal = which is the value I want to upload to my db

My question basically is how do I call my firstbillsubmitbal.php file and what/how do I edit on my php file so that my row_id and currentbal are uploaded on my db since I am no long using POST.


Thank you for the replies. So I went thru some SO answers and some tutorials I found on google and this is what happened to my js code.

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
var dealID = row_id;
//organize the data properly
var data = 'currentbal=' + currentbal.val() + '&dealID=' + dealID.val();

    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
        data: data,     
        cache: false,
        success: function() {  
        $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

And this is what happened to my firstbillsubmitbal.php page

$dealID = $_GET['dealID'] 
$currentbal = $_GET['currentbal']

$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID' LIMIT 1") or die (mysql_error());

But nothing happens when I click on the button to call my function. What am I missing?

Also, here is how I call my function. #COL1# is my row ID value.

<a href="javascript: funcEdit(#COL1#);" class="btn btn-success">Update</a>
Renee Cribe
  • 325
  • 1
  • 4
  • 14
  • Look into using [ajax](http://api.jquery.com/jQuery.ajax/) – SeanWM Jan 31 '13 at 02:51
  • The JavaScript code runs in the user's browser. The PHP code runs on the server. The database is on the server, so you need to use PHP code to do that. As @Sean mentioned, you can create a service that can be called using Ajax. – Steve H. Jan 31 '13 at 02:53
  • I edited my post due to the replies about AJAX. please note I am a beginner. Thanks =) – Renee Cribe Jan 31 '13 at 03:27
  • Try adding an error handeling to your $.Ajax call.. [see this](http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages) maybe there is an error. – Mortalus Jan 31 '13 at 03:32
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://www.brightmeup.info/article.php?a_id=2). – NullPoiиteя Jan 31 '13 at 03:35
  • show how you call your funcEdit – zb' Jan 31 '13 at 03:38
  • Thanks for the reply. I will look into PDO or MySQLi as I go along. OP edited to show how I call the function – Renee Cribe Jan 31 '13 at 03:44

2 Answers2

2

Are your function getting called correctly with row_id data ?
if so then might be this will give you a trick,

function funcEdit(row_id){  
var currentbal = document.getElementById('currentbal' + row_id).value;
var dealID = row_id;
    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
          //pass data like this 
        data: {currentbal:currentbal.val(),dealID: dealID.val()},    
        cache: false,
        success: function(data) {  
        if (data=="1")
        $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

and in php file

$dealID = $_GET['dealID'] 
$currentbal = $_GET['currentbal']

$sql = mysql_query("UPDATE deals SET 
        currentbal = '$currentbal',
        currentbalDone = 'Yes'
        WHERE deals.dealID = '$dealID' LIMIT 1") or die (mysql_error());

echo "1" ; // if update successful
else echo "0" // if update unsuccessful
sourcecode
  • 1,802
  • 2
  • 15
  • 17
  • thank you for the reply. Still nothing is happening. The function is being called properly because if I do a simply `alert(row_id);` or `alert(currentbal);` the value is correct. – Renee Cribe Jan 31 '13 at 04:04
  • did u checked that `ajax request is going to php page`? and are the variables `currentbal.val() , dealID.val()` is giving you correct value because you have described only `alert(currentbal);` – sourcecode Jan 31 '13 at 04:15
  • Sorry I am still new at this so to check if the currentbal.val() and dealID.val() is giving me the correct value I used document write(not sure if that is the correct way to do it though. But at first it wasn't passing the dealID value so I simply added a hidden field called dealID so when I document.write dealID it shows the correct value now. but it still is not submitting my php file. – Renee Cribe Jan 31 '13 at 04:33
  • I tried to assign the value manually to test it again by doing this`data: {currentbal:"50",dealID: "16"},` but it doesn't seem to be working either. How do I test if my ajax request is going to the php page? – Renee Cribe Jan 31 '13 at 04:35
  • press F12 key on your keyboard and then select net(in IE) / network(in mozilla) there whenever you click on your submit button ,there you will see request going. check this and then tell what is happening – sourcecode Jan 31 '13 at 04:58
  • well I seem to be getting an internal server error the link I see is `http://localhost/portal/firstbillsubmitbal.php?currentbal=50&dealID=16&_=1359608675622`, any idea what that 1359xxxxx is? – Renee Cribe Jan 31 '13 at 05:06
  • can you update your recent code you are using in your question, it'll help to determine the problem more – sourcecode Jan 31 '13 at 05:38
  • it works! it works! yey thanks very much for the f12 tip @sourcecode, seeing the error 500 I had to check why it wasn't seeing my php file that lead to check my sql db connect settings and after double check the values it finally worked!! thanks very much =) – Renee Cribe Jan 31 '13 at 05:43
0

HTML :

<form method="post" action="firstbillsubmitbal.php"> 
    <input name="currentbal" type="text" id="currentbal" class="input-mini"  />
    <a href="#" onclick="funcEdit(#COL1#); return false;" class="btn btn-success">Update</a>
</form>

JS :

function funcEdit(row_id){  

    var currentbal = $("#currentbal").val();

    //organize the data properly
    var data = 'currentbal=' + currentbal + '&dealID=' + row_id;

    //start the ajax
    $.ajax({
        url: "firstbillsubmitbal.php",  
        type: "GET",
        data: data,     
        cache: false,
        success: function() {  
            $('#message').html("<h2>Current balance has been updated!</h2>") 
        } 

    });             
}

Hope it works for you!

aimadnet
  • 440
  • 4
  • 12