I have simple code in php to enter name and address to the database.If name is already present in the database printing the message saying that it is already present ,if not entering data into database.But i want message has to be displayed near save button
<form method="POST" action="" >
<table>
<tr>
<td><br>Name t</td><td><br><input type="text" name="Name" id="wgtmsr" required/></td>
</tr>
<tr>
<td><br>Address</td>
<td><br/><textarea rows="3" cols="33" name="Address" id="inc_address" required></textarea></td>
</tr>
<tr>
<td><br></td><td><input type="submit" class="sav" name="rep" value="Save" id="btnsize" /></td>
</tr>
</table>
<?php
if (isset($_POST['rep']))
{
$Name=$_POST['Name'];
$Address=$_POST['Address'];
try{
$test=/* query check if name already present in database*/
if(!$test)
{
/*then insert into database*/
}else{
?> <div id="msg">
<?php echo "alreday present";?>
</div>
<?php
}
}catch(Exception $e) {
//echo "Error ".$e->getMessage();
}
}
But div is coming above the table.Please give suggestions so how can i style it near the save button
style:
#msg
{
color:#EA6262;
font-size:14pt;
margin-left:27% ;
margin-bttom:5% ;
}
Insert div after save button like
<input type="submit" class="sav" name="rep" value="Save" id="btnsize" /><span class="err" id="add_err"></span>
You can print the error using javascript.
document.getElementById("add_err").innerHTML = "Already having name!";
I tried similar thing its working
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 3/9/15
* Time: 4:03 PM
*/
?>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="submit" name="submit" /><span class="adderr" name="adderr" id="adderr"></span>
</form>
<?php
$var=0;
if($var==0)
{?>
<script>
document.getElementById("adderr").innerHTML = "Already having name!";
</script>
<?php
}
?>
</body>
</html>
It works fine
Related
****I'm trying to get submiitted value in database.After getting the value from a particular table i want to store the checked value into another table with the same columns.How to add the values that come from database while submitting after the checked valueHere's my code****
form.php
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<title></title>
</head>
<body>
<form action="insert.php" method="post">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="text">Sec</label>
<input type="text" class="form-control" name="sec" id="sec">
</div>
<button type="submit" name="submit" class="btn btn-default">Submit</button>
<button type="submit" name="getdata" class="btn btn-default">Get</button>
</form>
</body>
</html>
insert.php
<?php
$con=mysqli_connect("localhost", "root", "","input");
// inserting data
if(isset($_POST['submit'])){
$name=$_POST['name'];
$sec=$_POST['sec'];
if($name !=''||$sec !=''){
$query = mysqli_query($con,"insert input_form(Name,Sec) values ('$name', '$sec')");
echo "<br/><br/><span>Data Inserted successfully...!!</span>";
}
else{
echo "<p>Insertion Failed <br/> Some Fields are Blank....!!</p>";
}
}
else if(isset($_POST['getdata'])){
$query1 = mysqli_query($con,"select * from input_form");
while ($row1 = mysqli_fetch_array($query1)) {
?>
<ul class="form-get">
<input type="checkbox" name="chk[]" ><li><?php echo $row1['Name'];?><?php echo $row1['Sec']; ?> </li>
</ul>
<?php
}
?>
<button name="present">Submit</button>
<?php
}
?>
<?php
if(isset($_POST['present'])){
$checkbox=$_POST['chk'];
for($i=0;$i<sizeof($checkbox);$i++){
$query2=mysqli_query($con,"insert into present(Name,Sec) values ('".$checkbox[i]."')");
}
}
?>
<?php
mysqli_close($con);
?>
you should set a status for checked value to "1" and "0" to unchecked to store into database.Make a condition that if the variable is checked and have status to "1" then it will be submitted to database otherwise not.
here you can make a condition.
First of all you have missing a tag for check box in the form
<label for="checkbox">check</label>
<input type="checkbox" class="form-control" name="chk" id="chk">
here you can post the checkbox status
$chk=$_POST['chk'];
////////////create a column for check box also to make it easy./////
if(isset($_POST['present'])){
$checkbox=$_POST['chk'];
for($i=0;$i<sizeof($checkbox);$i++){
if($checkbox===1){
$query2=mysqli_query($con,"insert into present(Name,Sec,checkbox) values
('".$checkbox[i]."')");
}
}
}
hope it may help you.
when i refresh page auto submit . how to stop insert data when page refresh ? i want insert data when click add button and without redirect another page.
plz help me. i want to came page display show data.
<?php
try{
$host='localhost';
$name='pdo';
$user='root';
$pass='';
$dbc = new PDO("mysql:host=$host;dbname=$name",$user,$pass);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
//---------------------------------------------------------------------
if (isset($_POST['add']))
{
$stmt = $dbc->prepare("insert into user (frist,last,web) values (:frist,:last,:web)");
$stmt->bindParam(':frist',$_POST['frist']);
$stmt->bindParam(':last',$_POST['last']);
$stmt->bindParam(':web',$_POST['web']);
$stmt->execute();
}
?>
<!DOCTYPE html>
<html>
<head>
<title> the digital craft - PDO</title>
</head>
<body>
<header>
<h1>Mysql PDO</h1>
</header>
<div>
<form method="post">
<input name="frist" placeholder="frist name">
<input name="last" placeholder="last name">
<input name="web" placeholder="web site">
<button name="add" type="submit">Add</button>
</form>
</div>
<hr>
<table>
<thead>
<tr>
<th>ID</th>
<th>Frist Name</th>
<th>Last Name</th>
<th>Website</th>
<th>Save</th>
</tr>
</thead>
<tbody>
<?php
$stmt=$dbc->query("SELECT * FROM user");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
while($data=$stmt->fetch()){
?>
enter code here<tr>
<td><input name="id" value="<?=$data['id']?>"></td>
<td><input name="frist" value="<?=$data['frist']?>"></td>
<td><input name="last" value="<?=$data['last']?>"></td>
<td><input name="web" value="<?=$data['web']?>"></td>
<td><button name="save" type="submit">save</button></td>
</tr>
<?php } ?>
</tbody>
</table>
</body>
</html>
You could redirect the user to other/same page after submit logic has been run. After that, refreshing shouldn't submit the form again.
A little addition to what Siim said:
you can do this with header("Location: name_of_file.php"); after $stmt->execute();
I have problem in this code.
In this code when i press save data button , the data insert into database but when i refresh page then it's automatically insert into database, what should i do in my code then stop automatically insert data into database, thanks in advance
<?php
require './database/databaseConnection.php';
if(isset($_POST['save_button']))
{
if(($_POST['fname']&&($_POST['lname'])))
{
$first_name=$_POST['fname'];
$last_name=$_POST['lname'];
$qry="INSERT INTO user_master(first_name,last_name) values('$first_name','$last_name')";
$result= mysql_query($qry)or die(mysql_error());
if($result){
echo 'SuccessFully saved data';
}
else{
echo 'Data Not Inserted!';
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link rel="stylesheet" href="bootStrap/css/bootstrap.min.css">
<link rel="stylesheet" href="bootStrap/css/bootstrap.css">
</head>
<body>
<div class="container jumbotron ">
<form action="" method="post">
<table class="table-responsive">
<div class="form-group form-inline">
<tbody>
<tr>
<td> <label for="fname" class="label-info">First Name</label></td>
<td><input class="form-control" type="text" name="fname"></td>
<td><label for="lname" class="label-info">Last Name</label></td>
<td><input class="form-control" type="text" name="lname"></td>
</tr>
</tbody>
</div>
</table>
<button type="submit" name="save_button" class="btn-success" >Save Data</button>
</form>
</div>
</body>
</html>
This is happening because your action is empty
Update your action to this
action="<?php echo $_SERVER['PHP_SELF']; ?>"
Make a separate php file that will insert data to database. Give this in the form action attribute.
<form action="insert.php" method="post">
......
......
</form>
insert.php file
<?php
require './database/databaseConnection.php';
if(isset($_POST['save_button']))
{
if(($_POST['fname']&&($_POST['lname'])))
{
$first_name=$_POST['fname'];
$last_name=$_POST['lname'];
$qry="INSERT INTO user_master(first_name,last_name) values('$first_name','$last_name')";
$result= mysqli_query($qry)or die(mysql_error());
if($result){
echo 'SuccessFully saved data';
}
else{
echo 'Data Not Inserted!';
}
}
}
?>
You can use header() to redirect to your previous page if you want. Thus not allowing the refreshing of insert.php
header("location: your_page.php");
it will be safe if you use Prepared Statements
Take a look
I'm just learning how to do this so please forgive my ignorance!
Here is my test site: http://webtestkit.com/1KaraokeDJ/index.php
First, I found this code in a learning example and that code worked just fine. (http://www.mostlikers.com/2013/08/search-engine.html) - no problems. I even made the sample database and checked all was working.
now I wanted to change it to work for my purpose (a karaoke search)...
here is my code:
<?php
include("connect.php");
session_start();
if(isset($_POST['submit']))
{
$search=$_POST['search'];
$_SESSION['title']= $search;
if(($_SESSION['title'])!="")
{ header("location:index.php"); }
else
{ echo "<script> alert('Please enter something to search for') </script>"; }
}
?>
<html>
<head>
<title>1KaraokeDJ.com</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="login">
<form method="post">
<p><img src="top.jpg" /></p>
<p>
<?php if(isset($_SESSION['title'])) { ?>
<input name="search" type="search" list="searchkey" value="<?php echo $_SESSION['title'];?>" class="search" />
<?php } else { ?>
<input name="search" type="search" list="searchkey" placeholder="Just type your text here and press enter - ex : Abba" class="search" />
<?php } ?>
</p>
<datalist id="searchkey">
<?php
$tile=$db->query("SELECT * FROM `1KaraokeDJ.com`");
while($storetitle=mysqli_fetch_object($tile))
{ ?>
<option value="<?php echo $storetitle->title ?>">
<?php } ?>
</datalist>
<p><input type="submit" name="submit" id="click" class="searchbutton" value="Karaoke Search" /></p>
<?php if(isset($_SESSION['title'])) {
if(($_SESSION['title']!=""))
{
$data=$_SESSION['title'];
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{
while($descri=mysqli_fetch_object($view))
{
?>
<div class="reslt">
<h3 id="resuil-title"><?php echo $descri->title; ?></h3>
<p class="Description">
<?php $description = str_replace($data, '<span class="highlight">'.$data."</span>", $descri->artist);
echo $description; ?>
<p>
<hr>
</div>
<?php } } else { ?>
<div class="reslt">
<h3 id="resuil-title">Nothing fond!</h3>
<p class="Description">Try changing your search terms<p><hr>
</div>
<?php } } } ?>
</form>
</div>
</body>
</html>
The search field is finding data in the dropdown list so the connection is fine and the search works.
I think I understand most of the code, but I don't seem to understand this:
$view=$db->query("select * from 1KaraokeDJ.com where title like '%$data%' limit 10");
$check=mysqli_num_rows($view);
if($check!="")
{ while($descri=mysqli_fetch_object($view)) {
My If always goes to else "Nothing Found"
Any help? The way I learn is by doing - so it's trial and error until I figure it out!
Your table 1KaraokeDJ.com is interpreted as DatabaseName.TableName
To avoid this, escape your table name
Select * from `1KaraokeDJ.com` Where ...
I'm having trouble with these files, see if you guys can help me out :)
I get an object required error..
index.php
<?php
//Start session
session_start();
//Require functions.php
require("functions.php");
//Require posts.php
require("posts.php");
?>
<html>
<body>
<title>Log Into Booking System</title>
<?php
//Call function to display login
displayLogin();
?>
</body>
</html>
posts.php
<?php
if (isset($_POST['sublogin']))
{
//Check that all fields were typed in and are not null
if(!$_POST['user'] || !$_POST['pass'])
{
//Call function to display error
printText("Please make sure all required fields are filled in.");
}
else
{
//Here is some database checking
}
}
?>
functions.php
<?php
function displayLogin()
{
//Break php, then return to PHP
?>
<h1>Login</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td><div id='error'></div></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
<?
}
function printText($txtmsg)
{
?>
<!-- Set up the displayError javascript function -->
<script type="text/javascript">
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
</script>
<!-- Call function to Display the error -->
<script type="text/javascript">
displayError();
</script>
<?
}
?>
I'm pretty sure
document.getElementById('error').innerHTML
Is causing me problems, i think it might have to do with how im calling it, like it dosent know what its looking for..
Thanks guys much appreciated,
Jenny
EDIT: Here is the paste of what jQuerybeast tried to do, http://pastebin.com/jj0RVMAd
SOLUTION: Who knew it was this easy? You MUST change the order of where posts.php is required as well as make sure you don't call a javascript function within functions.php because that will execute the javascript above the form and therefore giving a null object error.
Index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Log Into Booking System</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<?php
//Start session
session_start();
//Require functions.php
require("functions.php");
?>
</head>
<body>
<?php
//Call function to display login
displayLogin();
//Require posts.php
require("posts.php");
?>
</body>
</html>
posts.php
<?php
//POST: Main login page
if (isset($_POST['sublogin']))
{
//Display error if feilds are not filled in
if(!$_POST['user'] || !$_POST['pass'])
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure all required fields are filled in.";</script>';
return false;
}
else
{
//Some database minipulation here....
//...
if ($_POST['user'] != $databaseUsername)
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your username is correct.";</script>';
return false;
}
elseif($_POST['pass'] != $databasePassword)
{
echo '<script type="text/javascript">document.getElementById("error").innerHTML = "Please make sure your password is correct.";</script>';
return false;
}
}
return true;
}
?>
functions.php
<?php
//Function: Display main.php login
function displayLogin()
{
//Break php, then return to PHP
?>
<div style="float: left; padding: 10px; width:20%;">
<fieldset>
<legend>
<h1>Login</h1>
</legend>
<form name = "mainlogin" action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember">
<font size="2">Remember username and password</td></tr>
<tr><td><div id="error"></div></tr><td>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
</fieldset>
</div>
<?php
}
?>
The first thing I notice is that the PHP-echoed string inside the Javascript function doesn't have quotation marks around it.
Change:
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
To:
function displayError()
{
document.getElementById('error').innerHTML = "<?echo $txtmsg;?>";
}
I think the problem is caused from functions.php is because you open and close PHP the opposite way.
Instead of ?> <?
Try: <? ?>
Which is this:
function displayLogin()
{
//Break php, then return to PHP
<?
<h1>Login</h1>
<form action="" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td><div id='error'></div></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sublogin" value="Login"></td></tr>
</table>
</form>
?>
}
function printText($txtmsg)
{
<?
<!-- Set up the displayError javascript function -->
<script type="text/javascript">
function displayError()
{
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
}
</script>
<!-- Call function to Display the error -->
<script type="text/javascript">
displayError();
</script>
?>
}
change
document.getElementById('error').innerHTML = <?echo $txtmsg;?>;
to
document.getElementById('error').innerHTML = '<?= $txtmsg; ?>';
or
document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';
Use this
document.getElementById('error').innerHTML = '<?php echo $txtmsg; ?>';
and
It seems short tag is not enabled on your server. Please make sure.
Or always use <?php ?> instead of <? ?>