Calling PHP function wth HTML button - javascript

I am developing website using php.I need to call php function using HTML Onclick event. How to do? Here I have given my code.
<?php
function insert() {
echo "in insert ";
$servername = "localhost";
$username = "shwetharao";
$password = "shwetha";
$dbname = "semilab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$uname = $_POST['usernameu'];
$upass = $_POST['passwordu'];
echo $uname;
$sql = "INSERT INTO login (id, username, password)VALUES ('id','$uname','$upass')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="button" type="button" value=" Create user " onclick="insert();">
</form>
</div>
</div>
</body>
</html>
I need to call function insert()[which is php function] on clicking button.How to achieve this?

Try this
<?php if(isset($_POST['submit'])){
insert();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="submit" type="submit" value=" Create user" >
</div>
</div>
</body>
</html>

you did two mistake first one is php tag at above, you wrote only "?" please change it to '<form action="" method="post">
inside of form you don't write action page name ..for example action = "myFile.php"

PHP: Is only run by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).
HTML & Javascript: Is only run in someone's browser
I'm assuming your file looks something like:
<?php
function insert() {
echo 'I just ran a php function';
}
if(isset($_POST['submit'])){
insert();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="submit" type="submit" value=" Create user" >
</div>
</div>
</body>
</html>

Just change
<input name="button" type="button" value="Create user" onclick="insert();">
to
<input name="button" type="submit" value="Create user">
And paste your php code inside this
<?php if(isset($_POST['submit'])){
//paste you php code here
}
?>

<?php
if($_GET){
if(isset($_GET['insert'])){
insert();
}elseif(isset($_GET['select'])){
select();
}
}
function select()
{
echo "The select function is called.";
}
function insert()
{
echo "The insert function is called.";
}
?>
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
===============================================
<?php
if($_GET['button1']){fun1();}
if($_GET['button2']){fun2();}
function fun1()
{
//This function will update some column in database to 1
}
function fun2()
{
//This function will update some column in database to 2
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<button id="btnfun1" name="btnfun1" onClick='location.href="?button1=1"'>Update to 1</button>
<button id="btnfun2" name="btnfun2" onClick='location.href="?button2=1"'>Update to 2</button>
</body>
</html>
I did it using this code. Hope this helps.

Related

How to submit POST requests if form is a value of innerHTML?

Every time I access $_POST on the other page, there is no value. How do we submit post request if the form is in innerHTML?
function edit(firstname){
var firstname_old = document.getElementById("firstname-div")
//Set id="FirstName" to your input field
firstname_old.innerHTML= 'First Name:<input type="text" id="FirstName" name="firstname" value="'+ firstname +'">';
}
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
When you are not in "edit-mode" your're putting out just a text.
<?php echo "First Name: $firstname<br>"; ?>
Thist text is inside the form which will be submitted but its not a form-field! To achieve what you want (deliver the content of "firstname" via ajax) you need a "hidden field".
So you put the same value which is shown in the text to a hidden input like this.
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="hidden" name="firstname" value="'<?php echo $firstname ?>'">
<input type="submit" name="submit" value="submit">
</form>
What I understand that you want to display a textbox to edit the name on clicking the edit button . Here is one solution for that:-
<script>
$(document).ready(function(){
$("#edit").click(function(){
document.getElementById("firstname-div").style.display = "none" ;
document.getElementById("firstname-div-edit").style.display = "block" ;
});
});
</script>
<HTML>
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<div id="firstname-div-edit" style="display:none;">
<input type="text" name="firstname" value="'<?php echo $firstname ?>'">
</div>
<button type="button" id="edit">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
</HTML>
Check and let me know if it works for you or not.

Stuck on login page after re-styling of login form using css and js

I know there was a similar question posted before and I already go through it, here's the link Login code doesn't work, stay stuck on log in page but our code is totally different and I already go through it for 3 hours but come with no solution. Here's what my code initially look like before I style it.
<body>
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
$username = stripslashes($_REQUEST['username']); // removes backslashes
$username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
header("Location:../user/page/emergency.php"); // Redirect user to index.php
}else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="form">
<h1>Log In</h1>
<form action="" method="post" name="login">
<input type="text" name="username" placeholder="Username" required />
<input type="password" name="password" placeholder="Password" required />
<input name="submit" type="submit" value="Login" />
</form>
<br /><br />
</div>
<?php } ?>
</body>
and here is what it look like after I try to style it using css and javascript.
<body>
<h2> Selamat Datang, Sila Log Masuk Untuk Kemaskini</h2>
<?php
require('db.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['username'])){
$username = stripslashes($_REQUEST['username']); // removes backslashes
$username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
$password = stripslashes($_REQUEST['password']);
$password = mysqli_real_escape_string($con,$password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE username='$username' and password='".md5($password)."'";
$result = mysqli_query($con,$query) or die(mysql_error());
$rows = mysqli_num_rows($result);
if($rows==1){
$_SESSION['username'] = $username;
header("Location:../user/page/emergency.php"); // direct user to update page
}else{
echo "<div class='form'><h3>Username/password is incorrect.</h3><br/>Click here to <a href='login.php'>Login</a></div>";
}
}else{
?>
<div class="container">
<section id="content">
<form action="" method="post" name="login">
<h1>Daftar Masuk</h1>
<div>
<input type="text" placeholder= "Username" required ="" id ="username"/>
</div>
<div>
<input type="password" placeholder= "Password" required ="" id ="password"/>
</div>
<input name="submit" type="submit" value="Login" />
</form>
</section>
</div>
<?php } ?>
<script src="js/index.js"></script>
</body>
and once I style it, the form stay stuck on the login page. Help me, what should I do? Any suggestion and guidance is greatly appreciated.
updated:
okay guys, I found the source of the problem already, I just need to remove on the second form.
Turns out, on the form where I do my styling, I just need to remove the tag
so from
<div class="container">
<section id="content">
<form action="" method="post" name="login">
<h1>Daftar Masuk</h1>
<div>
<input type="text" placeholder= "Username" required ="" id ="username"/>
</div>
<div>
<input type="password" placeholder= "Password" required ="" id ="password"/>
</div>
<input name="submit" type="submit" value="Login" />
</form>
</section>
</div>
to
<div class="container">
<section id="content">
<form action="" method="post" name="login">
<h1>Daftar Masuk</h1>
<input type="text" placeholder= "Username" required ="" id ="username"/>
<input type="password" placeholder= "Password" required ="" id ="password"/>
<input name="submit" type="submit" value="Login" />
</form>
</section>
</div>

Use PHP to display user inputs

What I'm trying to do is to use PHP to display the user inputs on a separate page, but when I process the form none of the input values display. I'm having to use javascript for input validation and that works fine but the PHP page is not working.
Here is my code:
HTML and Javascript
survey.php:
<!DOCTYPE html>
<html>
<head>
<title>Survey Page</title>
<link rel="stylesheet" type="text/css" href="mystylepage.css" />
<script type="text/javascript">
function validate()
{
if (document.information.name.value == "" )
{
alert("Please provide your name!" );
document.information.value.focus();
return false;
}
if (document.information.email.value == "")
{
alert("Please provide an email!");
document.information.value.focus();
return false;
}
if (document.information.major.value == false)
{
alert("Please enter your major");
document.information.value.focus();
return false;
}
return true;
}
</script>
</head>
<div id="bodycolor">
<body>
<div id="container">
<div id="menu">Menu:
<a class="menu" href="index.htm">Home</a> | <a class="menu"
`href="faculty.htm">Faculty</a> |
<a class="menu" href="courses.htm">
Courses</a> | <a class="gradecalculator" href="gradecalculator.htm">Grade
Calculator</a> | <a class="survey" href="survey.htm">Survey</a>
</div>
</div>
<div id="header">
<h1><center>CSE Center for Health Information Technology</center></h1>
<br>
<div id="links">
<center><a href="https://www.kennesaw.edu/"><img src="KSU Logo.jpg"
alt="Logo" style="width:100px;height:100px;" /></a></center>
<br>
<center><a href="http://ccse.kennesaw.edu/">College of Computing and
Software Engineering</a></center>
</div>
</div>
<h1>Please enter you information below</h1>
<form name="information" method="POST" action="action.php"
onsubmit="return
validate();">
<table width="500" border="0">
<tr>
<td width="150" bgcolor="#99CCFF"><strong>Name</strong></td>
<td><input type="text" name="name" size="20" maxlength="20" /></td>
</tr>
<tr>
<td bgcolor="#99CCFF"><strong>Email</strong></td>
<td><input type="text" name="email" size="20" maxlength="20" /></td>
</table>
<p> Major:
<input name="major" type="radio" value="Information Technology" />
Information Technology
<input name="major" type="radio" value="Software Engineering" />
Software Engineering
<input name="major" type="radio" value="Computer Science" />
Computer Science</p>
<p>
<input type="submit" value="Process" />
</p>
<p id="msg"></p>
</form>
</body>
</html>
Here is the PHP page:
action.php
<!DOCTYPE html>
<html>
<head>
<title>Survey Information Processing</title>
</head>
<body>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]?></p>
<p>Email: <?php echo $_POST["email"]?</p>
<p>Major: <?php echo $_POST["major"]?</p>
</body>
</html>
I'm totally new to PHP, so any help is greatly appreciated!
Your form method should be POST.
<form name="information" method="POST" action="action.php" onsubmit="return validate();">
And add a name to the submit button:
<input type="submit" value="Process" name="submit" />
Also, you could add this if condition:
<?php
echo "<pre>";
print_r($_POST); // Check if you're getting any POST data
echo "</pre>";
?>
<?php if (isset($_POST['name'])) { ?>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]; ?>
</p>
<p>Email: <?php echo $_POST["email"]; ?>
</p>
<p>Major: <?php echo $_POST["major"]; ?>
</p>
<?php } ?>
Hope this helps.
So the issue is your method is get but you're trying to access through post
Change
method="GET"
to
method="POST"
On your form.

html form,db submission,empty data on refresh or back

I have a from submitting data to db,and after entering in to db thank you message comes,everything is in same page.the problem is wheni refresh or go back the same data or empty data is entered in to db. Please help`
<?php
$myServer = "localhost";
$myUser = "root";
$myPass = "";
$myDB = "sample";
$name=$_POST['fname'];
$mobile=$_POST['mobile'];
$email=$_POST['email'];
//connection to the database
$dbhandle = mysql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
//echo "$myDB";
//Insert data into db
mysql_query("INSERT INTO reg(name,mobile,email) values ('$name','$mobile','$email')");
/* echo "$name";
echo "$mobile";
echo "$email"; */
//header("location:thank.html");
?>
<!doctype html>
<html>
<head>
<title>Basic form with validation</title>
<link rel="stylesheet" href="style/style.css" type="text/css" media="all"/>
</head>
<body>
<div style="width:300px; background:#19a3d1;margin:0 auto;height:200px;">
<?php
if(empty($_POST))
{
?>
<form name="myform" id="frm1" action="index.php" method="post" style="margin:0 auto;"onsubmit="return(validate());">
<h1 style="text-align:center">Form</h1>
<div style="display: table; margin:0 auto;">
<div style="display: table-row;">
<div style="display: table-cell;padding:5px;">
<label>Name</label>
</div>
<div style="display: table-cell;">
<input type="text" name="fname" id="fname" placeholder="Name" />
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;padding:5px;">
<label>Mobile</label>
</div>
<div>
<input type="text" name="mobile" id="mobile" placeholder="Mobile" />
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;padding:5px;">
<label>Email</label>
</div>
<div>
<input type="text" name="email" id="email" placeholder="Email" />
</div>
</div>
<div style="display: table-row;">
<div style="display: table-cell;padding:5px;" ></div>
<div style="display: table-cell;padding:5px;"><input type="submit" value="Submit"></div>
</div>
</div>
</form>
<?php
}
else{
?>
<div id="thank" >
<span style="color:#ff0000;padding:30px;text-align:center;display:block;">Thank you!!</span>
</div>
<?php
}
?>
</div>
<script src="js/main.js"></script>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</body>
</html>`
Give name of your submit button.
Like
<input type="submit" value="Submit" name="btnSubmit">
And Write PHP Code like this :
<?php
......
......
if(isset($_POST['btnSubmit']))
{
Your code....
}
?>
Or
You can use ajax to submit the form data.
http://www.formget.com/submit-form-using-ajax-php-and-jquery/

Why javascript not showing value in textfield

<html>
<head>Testing</head>
<body>
<?php
if(isset($_POST['postbtn'])){
echo "<script>alert('I am here')</script>";
echo "<script>document.getElementById('txt_name').value='edit'</script>";
}
?>
<div id="wrap">
<form id="data_entry" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="rowbox">
<input type="text" class="textbox" id="txt_name" value="">
</div>
<div class="btn">
<input type="submit" id="postme" name="postbtn">
</div>
</form>
</div>
</body>
</html>
I'm new to javascript and php, what I need to know is after submitting the form I can see the alert message "I am here" but I don't see the value that I am posting using the document.getElementById in the textfield for the html form. Why?
Thanks
Because the element has not loaded yet, move your script to the end of the body.
<?php
if(isset($_POST['postbtn'])){
echo "<script>alert('I am here')</script>";
echo "<script>document.getElementById('txt_name').value='edit';</script>";
}
?>
</body>
</html>

Categories

Resources