Onclick Insert To SQL - php

emphasized texti need help creating an onclick() function for an tag, that when it is activated it writes to 3 different rows in an MSSQL DB.
Im trying to figure it out, but my knowledge is not that great, i know i might have to use AJAX for this work.
Using MSSQL 2017, PHP 7.1
MY end result is to create a Click counter, the name of the section clicked and the date/time it was clicked.
IF anyone can help me, i appreciated very much.
Thank you.
Thanks for the guidance!!!
Edit
test.php
$counterServer = "TEST\TEST";
$counterconnection = array( "Database"=>"TestingCounters","UID"=>"User","PWD"=>"1234","CharacterSet"=>"UTF-8");
$finalcon = sqlsrv_connect( $counterServer, $counterconnection);
if( !$finalcon ) {
die( print_r( sqlsrv_errors(), true));
}
$sequel = "INSERT INTO dbo.CounterTest(Visit,Date_Value,Section)
VALUES('1',getdate(),'Kitchen')";
and my dumb logic , but i later realized i dont need to use a button but an anchor.
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<button action="test.php" method="post">click</button>
</body>
</html>
Final Working Test HTML
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<script type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js">
</script>
<script type="text/javascript">
function ctest(){
$(document).ready(function () {
$.post('test.php', // url
{ Section: 'something.' });
});
}
</script>
</head>
<body>
<h1> jQuery post() method demo
</h1>
click me
<p>
</p>
</body>
</html>

Use Jquery library for Ajax request then add the onclick event on the button to make the ajax request.
https://www.tutorialsteacher.com/jquery/jquery-post-method
You can see the example of ajax on this link. The url should be where your php script is to send the 3 different rows in an MSSQL DB.

Related

Handle html code in comment box

I have a website where users leave a comment which will be inserted into database.
After that the user will be redirected to the same page with updated and inserted comment display.
I used php and mysql for this. It is working fine.
But for safer side, I inserted html code in textbox and it is executing.
My html code is:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick=myFunction(Harry Potter,Wizard)>Try it</button>
<script>
function myFunction(name,job)
{
alert(Welcome + name +, the + job);
}
</script>
</body>
</html>
In my page try button is coming with an alert message.
I don't have any idea how to handle this.
Can any one help me?
Use quotes. jsfiddle
<!DOCTYPE html>
<html>
<body>
<p>Click the button to call a function with arguments</p>
<button onclick='myFunction("Harry Potter","Wizard")'>Try it</button>
<script>
function myFunction(name,job)
{
alert("Welcome" + name +", the" + job);
}
</script>
</body>
</html>

$.get command in jQuery goes wrong

I am new in jQuery and need help to figure out why $.get does not reply.
Let me explain what I have: There is a main index.php as follows:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="utf-8"> </head>
<body>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/reqhan.js"> </script>
<input id="string" type="text" />
<input id="button" type= "button" value="Go" />
<div id="div"></div>
</body>
</html>
the js/reqhan.js contains
$(document).ready(function(e) {
alert('1');
$('#button').click(function() {
$.get('php/reverse.php',{input: string},function(data){alert('2');});
$('#div').text(data);
alert('3');
});
});
and reverse.php contains a simple code (I pasted here but does not preview it) that gets the text from reqhan.js file and returns an echo message.
when running the code on Google Chrome, the first alert is shown but not the rest and of course the `$('#div').text(data);' doesn't send back the data to the js file.
Please let me know if further info is required.
many thanks.
You're closing your callback function before you do anything with the data
Try this instead:
$(document).ready(function(e) {
alert('1');
$('#button').click(function() {
$.get('php/reverse.php',{input: string},function(data){
alert('2');
$('#div').text(data);
alert('3');
});
});
});
Try to format your code so that each pair of brackets gets its own indentation. It should help catch small things like this.

How to show cursor first line on textbox

I want to show cursor first line of textbox if I press enter key. But always my cursor show in second line. please anyone can help me solving this problem. thanks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#txt_message').keydown(function (e){
if(e.keyCode == 13 ){
$('#txt_message').val("");
}
})
});
</script>
</head>
<body>
<textarea id='txt_message'></textarea>
</body>
</html>
$('#txt_message').keydown(function (e){
if(e.keyCode == 13 ){
e.preventDefault();//use this to prevent default behavior
$('#txt_message').val("");
}
demo js fiddle

ajax during onchange function

I've a really simple case , but made me confused , here is script =>
<!DOCTYPE html>
<html>
<head>
<title>HI</title>
<link rel="stylesheet" href="./scripts/css/default.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form name="ok" method="post" action="index.php">
<select name="sel" id="sel"><option value="a">1</option><option value="b">2</option><option value="c">3</option></select>
</form>
FIRST PAGE
<div id="as"></div>
<script type="text/javascript">
document.getElementById("sel").onchange = function(){
var doc = false;
if (window.XMLHttpRequest){
doc = new XMLHttpRequest();
}
if(doc){
doc.open("POST","./index.php",true);
doc.onreadystatechange = function(){
if (doc.status==200 && doc.readyState==4){
document.getElementById("as").innerHTML = doc.responseText;
}
};
doc.send("sel=" + document.getElementById("sel").value);
}
};
</script>
<?php
if (isset($_POST['sel'])){
echo $_POST['sel'];
}
?>
</body>
</html>
from this script I expect that onselect change returns me value of the select element , but it returns whole page again , why ? any ideas ? please help , thanks :))
PS. this is self index.php page
Create a separate php file to handle to your ajax requests that way ajax request will only show you the the texts echoed out by the php file.
This is my example code:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if (isset($_POST['sel'])){
echo "yeah";
}
}else{die('You are not allowed to access this page!');}

Pass global javascript variables to php

I have a index.php file where I have canvas game. This game is loaded from separate game.js file where I have variable: ballsCought. I want this wariable and name inputet to text input pass on click to another php file. my code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple Canvas Game</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
function score(){
$('#score').fadeIn(1000);
$('#score').load("load_players.php");
};
setInterval(score , 1000);
var nam = $("#name").val();
$('#submit').keyup(function(e){
if(e.keyCode == 13){
$.post('upload_score.php','n=' +nam, 'score=' +ballsCought);
}
});
$('#submit').click(function(e){
$.post('upload_score.php','n=' +nam, 'score=' +ballsCought);
});
});
</script>
</head>
<script src="game.js"></script>
<body>
<canvas id="canvas" width="525" height="525"></canvas>
<br /><p><span id="points"></span><input type="text" id="name" placeholder="Name..."/><input type="submit" id="submit" value="Submit"/></p>
<br /><span id="score"></span>
</body>
</html>
But this post function is not working any idea? THank you...
Looks like your $.post method is not correct. If you want to pass data to the PHP page you need to use the JavaScript object notation like so:
$.post('upload_score.php', {n: nam, score: ballsCought});
You can read more about the various ways to call $.post from the jQuery Docs page
Now there could still be problems with your PHP page. You should use something like Firebug to see the Ajax request and any errors that might be returned.

Categories