sql query on button click - php

i dont know whats wrong with my codes. kinda new to PHP
here is my html code
<html>
<body>
<form action="sample2.php" method="POST">
<input type="submit" value="occupied" id="occupied">
<input type="submit" value="reserved" id="reserved">
<a name="slot1" style="background-color: green; width:100px; height:100px; border-top-right-radius:0px; border: 2px
solid Black;float:left; position:absolute; top:400px; left:441px;">
</form>
</body>
</html>
PHP code is here
<?php
$con=mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample");
if(isset($_POST['occupied'])){
$query="UPDATE reservation SET status='occupied' where status='vacant'";
echo "<a style=background-color: red; width:100px; height:100px; border-top-right-radius:0px; border: 2px
solid Black;float:left; position:absolute; top:400px; left:441px;>";
}
?>
my problem is whenever i click the occupied button, the color of the box doesnt change from green to red, it only directs me to a blank page. help

Try this:
<input type="submit" value="occupied" name="occupied">
<input type="submit" value="reserved" name="reserved">
Forms submit controls with a "name" and since those didn't have the name attribute, they aren't being submitted to your PHP script.
Read about Successful Controls to learn more.

you have to set together the id and the name into the form like this:
<html>
<body>
<form action="sample2.php" method="POST">
<input type="submit" value="occupied" id="occupied" name="occupied">
<input type="submit" value="reserved" id="reserved" name="reserved">
<a name="slot1" style="background-color: green; width:100px; height:100px; border-top-right-radius:0px; border: 2px
solid Black;float:left; position:absolute; top:400px; left:441px;"></a>
</form>
</body>
</html>

Related

login page with PDO issue

I am creating a web page using php, there is no php error from the code, but when i click the button, it didnt do anything to the page. And i have no idea what is happening, if anyone can help that would be great thanks.
<?php
session_start();
if( isset($_SESSION['user_id']) ){
header("Location: /");
}
require 'database.php';
if (!empty($_POST['username']) && !empty($_POST['password'])):
$records=$conn->prepare('select id, username, password from admin where username = :username');
$records->bindParam(':username', $_POST['username']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$message='';
if(count($results)>0 && password_verify($_POST['password'], $results['password'])){
$_SESSION['user_id']=$results['id'];
header("Location: /");
}else{
echo 'Sorry, wrong password';
}
endif;
?>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Comfortaa' rel='stylesheet' type='text/css'>
<title>Admin Login</title>
</head>
<body>
<?php if(!empty($message)): ?>
<p><?=$message ?></p>
<?php endif; ?>
<h1>Admin Login</h1>
<form id='login' method='POST' action='login.php'>
<label>
Username:
<input type='text' name='username' placeholder='Enter your usename' id='username' size='20' maxlength='100'>
</label>
<br>
<label>
Password:
<input type='password' name='password' placeholder='Enter your password' id='password' size='20' maxlength='100' >
</label>
<br>
<button type='button'>Login</button>
</form>
Register
<br/>
<style type='text/css'>
form{
font-family: Verdana;
width: auto;
text-align: center;
}
label {
width: 150px;
font-size: 170%;
length: 200px;
}
body{
background-color: lightblue;
text-align: center;
position: fixed;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -200px;
height:200px;
width:400px;
}
button{
padding:10px;
color:#fff;
background:#0098cb;
width:200px;
margin:20px auto;
margin-top:0px;
border:0px;
border-radius: 3px;
cursor:pointer;
}
input[type='text']{
height:30px;
outline:none;
padding:10px;
width:200px;
border-radius: 3px;
border:1px solid #eee;
margin:20px auto;
}
button:hover{
background:#00b8eb;
}
</body>
</html>
Your button type is button you need to change the type to submit.
Your button now :
<button type='button'>Login</button>
What your button should be
<button type='submit' name="btnName">Login</button>
then on your php script check if the button is set then do your proccessing
<?php
if(isset($_POST['btnName'])){
// then the php code you have
}
?>
Button type = 'button' vs. type = 'submit'
Submit:
The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.
button:
The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.
The reason that there was noting happening in your form is because you have the type=button then you don't have a client-side script that associated with it's events.

this is an issue concerning styling html elements

bellow is my code that is suppose to create a search box and then style it. For some reason the css properties that I try to give to the box does not apply. I tried having the css in a separate file and also in the script as shown bellow. What is the problem (I'm using netbeans and the file is a .php)
here is the code:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title>cats</title>
</head>
<body>
<form>
<input type="text" placeholder="Search me..." required>
<input type="button" value="Search">
</form>
<style type="text/css" media="screen">
form {
width:500px;
margin:50px auto;
}
.Search {
padding:8px 15px;
background:rgba(50, 50, 50, 0.2);
border:0px solid #dbdbdb;
}
.button {
position:relative;
padding:6px 15px;
left:-8px;
border:2px solid #207cca;
background-color:#207cca;
color:#fafafa;
}
.button:hover {
background-color:#fafafa;
color:#207cca;
</style>
<?php
// put your code here
?>
</body>
</html>
You are using CSS class selectors. Give the elements you want to style the appropriate class attributes (as alluded to by patricksweeney):
form {
width:500px;
margin:50px auto;
}
.search {
padding:8px 15px;
background:rgba(50, 50, 50, 0.2);
border:0px solid #dbdbdb;
}
.button {
position:relative;
padding:6px 15px;
left:-8px;
border:2px solid #207cca;
background-color:#207cca;
color:#fafafa;
}
.button:hover {
background-color:#fafafa;
color:#207cca;
}
<form>
<input type="text" class="search" placeholder="Search me..." required>
<input type="button" class="button" value="Search">
</form>

Buttons and texts move back to original place

I have set when user types something in the search box, the suggestions will appear.
When suggestions appear, it will push the buttons and texts below down as I set its position relative.
But when user delete those words in the search box and the suggestion list disappears, the buttons and texts below the suggestion list did not move back to the original place.
How can I let the buttons and texts below the suggestion list move back to the original place when user delete those words in search box?
This is my current code:
<?php
include 'script_suggestion.php';
include 'script_close_suggestion_box.php';
?>
<html>
<head>
<title>
Brandon's Search Engine
</title>
<style type="text/css">
#suggestion {
border: 1px solid black;
visibility: hidden;
position: relative;
background-color: transparent;
z-index: 10;
}
.suggestion a {
font-size: 12pt;
color: black;
text-decoration: none;
display: block;
width: 648px;
height: auto;
text-align: left;
padding: 10px;
}
.suggestion a:hover {
background-color: #dddddd;
width: 644px;
padding: 2px;
}
</style>
</head>
<body>
<form method="GET" action="search.php" name="q">
<table align="center">
<div>
<h1><center>Brandon's Search Engine</center></h1>
</div>
<div align="center">
<input type="text" name="q" id="q" class="q" style="height: 27px; width: 650px; padding: 2px" placeholder="Search Now"
onkeyup="getSuggestion(this.value)" autocomplete="off" autoFill="on" onblur="closeBox()"/>
<script>document.getElementById('q').focus()</script>
<div id="suggestionBox" style="visibility: collapse;">
<div id="suggestion" style="width: 645px; text-align: left; padding: 2px;">
</div>
</div>
</div>
<br />
<div align="center">
<input type="submit" value="Search" name="submit" style="height: auto; width: 60px; padding: 2px" />
<input type="reset" value="Clear" onclick="closeBox()" style="height: auto; width: 50px; padding: 2px" />
</div>
<div align="center">
Can't find your site? <br /> Insert here.
</div>
</table>
<input type="hidden" name="page" value="1" />
</form>
</body>
</html>
Thanks in advance.
On your function in your onkeyup, you can check the text value and if that was empty change css position of your element to original:
function getSuggestion(value){
if(!value.length()){
//change to original place
}
.
.
.
}
I hope to help

How to make button execute on same page but not the next page

Hi all Im a new learner for web development.
I have a page with a form after user click the submit button, then button will submit the form content to the next page to execute and save into database.
So that, my page will go to another page to execute and return to the landing page.
Eg: index.php and exec.php
index.php:
<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />
<p class="g-question">Why you love it?</p>
<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>
<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form>
exec.php
if ($_POST["g-product"] && $_POST["g-reason"] != "" )
{
$gproduct = $_POST["g-product"];
$greason = $_POST["g-reason"];
$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php';";
echo "</script>";
}
My question is, how can I execute the submit button in the index.php without going to the exec.php? (means to combine both in a .php)
It is because when user click submit button it goes to a blank page to execute which do not look nice.
anyone can help? Thanks!
=)
if (isset($_POST["g-btn"]))
{
$gproduct = $_POST["g-product"];
$greason = $_POST["g-reason"];
$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php';";
echo "</script>";
}
<html>
<form name="g-form" action="a.php" method="post" class="goat-vote" onsubmit="return validategForm()">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />
<p class="g-question">Why you love it?</p>
<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>
<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;"></form>
</html>
you can do like this... isset($_POST["g-btn"]) will check if it's get clicked or not?
Edit: Ignore this please, didn't read your question completely as it seems
Try something like this:
index.php
<form name="g-form" action="gbtn-exec.php" method="post" class="goat-vote" onsubmit="return validategForm()">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />
<p class="g-question">Why you love it?</p>
<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>
<?php if(!isset($_GET['hidesubmit'])): ?>
<input name="g-btn" class="vote-btn" type="submit" value="vote" style="margin-left:470px; cursor:pointer;">
<?php endif; ?>
</form>
exec.php
if ($_POST["g-product"] && $_POST["g-reason"] != "" )
{
$gproduct = $_POST["g-product"];
$greason = $_POST["g-reason"];
$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php?hidesubmit=1';";
echo "</script>";
}
Add jQuery lib in your head and your jQuery perso (myjQuery.js) that you will use after for ajax
index.php
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/js/myjQuery.js"></script>
Create your form without action on your form.
<html>
<form name="g-form" class="goat-vote">
<input type="text" name="g-product" placeholder="Brand / Product Name" style="-moz-border-radius: 5px; border-radius: 5px; padding-left:20px; opacity:.5; border:none; margin-left:110px; width:440px; height:38px; font-family:'Proxima Nova Rg';color:#000; font:1.6em;" />
<p class="g-question">Why you love it?</p>
<textarea name="g-reason" style="-moz-border-radius: 5px; border-radius: 5px; padding:5px; opacity:.5; border:none; margin-left:110px; width:450px; height:150px; font-family:'Proxima Nova Rg';color:#333; font-size:1em;"></textarea>
<input name="g-btn" class="vote-btn" value="vote" style="margin-left:470px; cursor:pointer;">
</form>
</html>
Keep your exec.php like this
exec.php
if ($_POST["g-product"] && $_POST["g-reason"] != "" )
{
$gproduct = $_POST["g-product"];
$greason = $_POST["g-reason"];
$insert ="INSERT INTO jovine.vote (vote_id ,product_name ,reason ,type) VALUES (NULL, '$gproduct', '$greason', 'goat')";
$result = mysql_query($insert,$con);
echo "<script>";
echo "alert('Thank you. Your vote has been recorded.');";
echo "window.location.href='index.php?hidesubmit=1';";
echo "</script>";
}
and create myjQuery.js
myjQuery.js
$(document).ready(function(){
$(".vote-btn").on("click", function(){
g-product = $("input[name='g-product']").val();
g-reason = $("input[name='g-reason']").val();
$.ajax({cache:false,dataType:'html',crossDomain:true,type:'POST',
url:'/php/exec.php',data : {g-product:g-product, g-reason:g-reason},
success:function(html){
console.log(html);
},
error:function(j,t,e){
console.log('problem');
}
});
});
});
You can try this code, maybe there are problems with js path or php path of your files. And maybe some mistakes that you can correct.

stuck with a simple php code

I have no experience with php at all. Some html, some javascript, but that is it.
All I want to do is have a drop-down list for database connections and then based on that connect to the database chosen. Simple enough but I can't seem to get the select option value. I've gone down a huge rat hole trying to figure this out. everything points me to isset($_POST['somevariable']) to get the value but I think I'm missing something key. Do i need to be using the form tag or something. no matter what I do, I can't get at the variable. Also, can I just do this all in one php script? other posts have multiple php scripts. I ended up adding a submit button to see if that is it... I just am coming up empty.. I'm missing something really dumb...
Here is my code:
<html>
<head>
<title>Liferay Utilities</title>
<style>
.label {
font-weight: bold;
margin: 10px 20px 10px 0;
float: left;
clear: left;
width: 200px;
}
.element {
margin: 10px 0;
width: 150px;
float: left;
}
</style>
</head>
<body>
<h1>Liferay Utilities</h1>
<hr style="border-bottom: #cac917 5px solid;" />
<p class="label">Choose Database:</p>
<select name="dbtype">
<option value="default">Select Database</option>
<option value="suntest">SUNTEST</option>
<option value="sundevo">SUNDEVO</option>
</select>
<input type="submit" name="submit" value="Next">
</p>
<?php
//just debugging
echo 'xx';
echo isset($_POST['submit']);
echo 'xx';
if (isset($_POST['dbtype']))
{
// I NEVER EVER GET IN HERE
echo 'in here';
}
?>
</body>
</html>
Any Help would be greatly appreciated.
HERE IS MY NEW CODE: IT WORKS NOW!!! Thank you all! Wish I had posted an hour ago... If there are any good websites out there were full examples and real-world ones, let me know.
<html>
<head>
<title>Liferay Utilities</title>
<style>
.label {
font-weight: bold;
margin: 10px 20px 10px 0;
float: left;
clear: left;
width: 200px;
}
.element {
margin: 10px 0;
width: 150px;
float: left;
}
</style>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<h1>Liferay Utilities</h1>
<hr style="border-bottom: #cac917 5px solid;" />
<p class="label">Choose Database:</p>
<select name="dbtype">
<option value="default">Select Database</option>
<option value="suntest">SUNTEST</option>
<option value="sundevo">SUNDEVO</option>
</select>
<input type="submit" name="submit" value="Next">
</p>
<?php
echo $_SERVER['PHP_SELF'];
//just debugging
echo 'xx';
echo isset($_POST['submit']);
echo 'xx';
if (isset($_POST['dbtype']))
{
// I NEVER EVER GET IN HERE
echo 'in here';
}
?>
</form>
</body>
</html>
Try:
<form action="page.php" method="post">
<select name="dbtype">
<option value="default">Select Database</option>
<option value="suntest">SUNTEST</option>
<option value="sundevo">SUNDEVO</option>
</select>
<input type="submit" name="submit" value="Next">
</form>
in your php do echo $_POST["dbtype"]; to see if you get the correct result.
Change page.php for the name of the page you are submitting the form to.
You not defining the method. At the very least you must have that in order to use $_POST. To do that you must wrap your input/select in a form
<form method="post">
<select name="dbtype">
<option value="default">Select Database</option>
<option value="suntest">SUNTEST</option>
<option value="sundevo">SUNDEVO</option>
</select>
<input type="submit" name="submit" value="Next">
</form>
Note: Without giving an action attribute, the action will become the current page.

Categories