Call update query from JS, can you help me?

Hi,

I am trying to add a button in the header of a form. By clicking on this button, I want to update the current record. Can you help me ?

Currently I was able to add from the javascript library, my button in the header :

$("#header").append("<div id='doublon'>My text here : <a href='#' id='button'>Click here to change date</a></div>");

I do not know where to put this code?

$update_sql = "UPDATE users SET date= NOW() WHERE id = {id}";
sc_exec_sql($update_sql);

In my javascript library it doesn’t work.

Thank you

You cannot update current record from javascript, since the javascript is running on the client and your database is on the server

You need to do an ajax call to post the information on your server

Create a new AJAX button.
and run your script.

@jlboutin60 Yes i know, so how do an ajax call from my library js ?

@alvagar So, i don’t want to create a ajax button, because my button appear from header actually

Thanks

It should look like this, adapt it for your situation

<script type="text/javascript">
    $(document).ready(function () {
	 $('#ajaxBtn').click(function(){
		$.ajax('..\blankapp\index.php', {
			type: 'POST',  
			data: { myData: 'This is my data.' },  // data to submit
			success: function (data, status) {
				$('p').append('status: ' + status + ', data: ' + data);
			},
			error: function (errorMessage) {
					$('p').append('Error: ' + errorMessage);
				}
		});
	});
});
</script>
<input type="button" id="ajaxBtn" value="Send Ajax request" />
<p>
</p>

I didn’t try it but maybe this can work too. Create an AJAX button, hide it and modify your javascript to activate it

2 Likes

Thank you jlboutin.

I have a problem with your code. The page is reloading after p tag. Do you have an idea how load page in current iframe ?

Thanks.

Did you keep the type = “button”, it normally prevent the page to reload

You can also do a return false; after the $.ajax call

I have been reading about jquery and ajax and now that I understand everything is good! A big thank you for your help!