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>
Related
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.
I am very new to web designing. I want to create a web page such that when the user is typing in the form, at the same moment that data must be shown on the left side of the same page as in the image below. Example, as the user types in the about me text box, the data area on the left side with about me title should also get updated instantly and dynamically with the same speed the user is typing.
Can anyone please help me how this is possible? or which language will i have to use to do this?
thanks in advance.
example image
You will have to use javascript to achieve this.
<input type="text" id="input">
<div id="output">
</div>
<script>
var input = document.getElementById('input');
var output = document.getElementById('output');
// add event listener to the input element
input.onkeyup= function() {
// set the output element HTML to what you type in
output.innerHTML = this.value;
}
</script>
Fiddle: https://jsfiddle.net/xpvt214o/124485/
Use the following code :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script>
$(document).ready(function(){
$('#name').keyup(function () {
$('#display').text($(this).val());
});
});
</script>
</head>
<body>
<input type="text" id="name"/>
<span id="display"></span>
</body>
</html>
I am trying to run this simple Ajax Post example like:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("test.php",
{
name:"Hello Ajax"
},
function(data){
$("p").html(data);
}
);
});
});
</script>
</head>
<body>
<p></p>
</body>
</html>
and PHP (test.php) as:
<?php
$name = $_POST['name'];
echo '<a>'.$name.'</a>';
?>
No error message but not getting any result back! can you please let me know what I am doing wrong?
Just a simple change, added button element in your markup. Click it and it should work:
<body>
<button>Click Me</button>
<p></p>
</body>
Your javascript is looking for the button element on which when clicked, will trigger the ajaxcall in test.php
$("button").click(function(){
By clicking the button, it will display Hello Ajax, the echo from the test.php and the one that you've send.
$.post("test.php",
{
name:"Hello Ajax"
},
Hello I need to make a form at test.html that on click changes index elements (title, image, description (text) and a href attribute) with input elements. Can you help me?
index.html
<html>
<body>
<p id="pal">This is a paragraph.</p>
<p>This is another paragraph.</p>
<button id="ch">Replace the first p element with new text</button>
</body>
</html>
test.html
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#ch").click(function(){
$("#pal").replaceWith(#in1);
});
});
</script>
</head>
<body>
<input id="in1" type="text"></input>
<input id="in2" type="text"></input>
</body>
</html>
Ok, if i understand well, you have an input tag where the user will write something, and when he clicks on the button, that content will go to another place.
Input where he writes:
<input type="text" id="user_text">
The place it should go once it is clicked:
<p id="pal">This is the target.</p>
The click button:
<button id="ch">Replace the first p element with new text</button>
The JQuery script should be:
$(document).ready(function(){
//on click
$("#ch").click(function(){
//get content from input (what user wrote)
var userContent = $("#user_text").val();
//put it inside the desired div
$("#pal").html(userContent);
//clean what user wrote
$("#user_text").val('');
});
});
If you want to see the result, here is the JSFiddle
I have some php code that generates a random password. Next to a text box I have some text which says "click to Generate a random password".
What I am looking to achieve is when the text in double quotes above is clicked that the PHP code is run and the generated random password is then pasted into the text box which is beside the text above in double quotes.
How could do this? I am thinking maybe jQuery but not sure how I would do what I want above.
Here you are... complete and working example :)
Put files 'index.html' and 'passgenerator.php' into same directory.
index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#generate').click(function(){
$.get('passgenerator.php', function(data) {
$('[name=password]').val(data);
});
})
});
</script>
</head>
<body>
<form action="">
<input type="text" name="password">
<input type="button" id="generate" value="Generate password">
</form>
</body>
</html>
passgenerator.php
<?php
echo sha1(uniqid());
?>
either use ajax to call the script page that generates the password or like col. shrapnel says port the generating function to javascript (which is a better idea)
Example (with jquery using ajax)
$.get("/PasswordGenerator.php",function(password)
{
$("#TextboxID").val( password );
});
where TextboxID is the id of the textbox u want the password to be added to.
Don't do it in php. Just do it in javascript. Here is a very neat tutorial that should show you the step by step instructions:
http://www.mediacollege.com/internet/javascript/number/random.html
Patrick Evans is right. You can setup your form like this :
<input type="button" value="Generate" onclick="generate();">
And in section you have to define a function
<script type="text/javascript">
function generate(){
$.get("/PasswordGenerator.php",function(password)
{
$("#TextboxID").val( password );
});
}
</script>
Try this ajax call to get the content using ajax and display it on the div.
$("#generate").click(function(){
$.get('url:to:urPHP',{
data:urData
},function(data){
$("#generatedPassword").html(data);
});
});
<input type="" id="generate" value="click to Generate a random password" />
<div id="generatedPassword">
<div>