I want to go to other php page on pressing html button of same site. I have tried it by using header function but it was not working.
Here is the simple html code:
<input type="button" name="Insert_Ad" value="Post and ad">
If user clicks this button it will take it to another php page For Example with URL adddress 'http://localhost/Product.php/Product.php'
Here is the code of that PHP page to which I want to go on pressing button
<html>
<head>
<meta charset="UTF-8">
<title>Insert form data</title>
</head>
<body>
<form method="post" action ="Product.php" id="contact-form">
<input type="text" name="product_category" placeholder="product_category" required />
<input type="text" name="product_name" placeholder="product_name" required />
<textarea id = "address" name="product_description" placeholder="product_description" required /></textarea>
<input type="text" name="product_image1" placeholder="product_image1" required />
<input type="text" name="product_image2" placeholder="product_image2" required />
<input type="text" name="product_image3" placeholder="product_image3" required />
<input type="text" name="product_image4" placeholder="product_image4" required />
<input type="text" name="product_image5" placeholder="product_image5" required />
<div class="btn-group" role="group">
<input type="submit" class="btn btn-default" name="Add" value="Enter the box" style="margin-top: 15px; margin-right: 15px; border-radius: 4px;">
</div>
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "zz224466";
$dbname = "zain";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Add']))
{
$product_category = $_POST['product_category'];
$product_name = ($_POST['product_name']);
$product_description = ($_POST['product_description']);
$product_image1 = ($_POST['product_image1']);
$product_image2 = ($_POST['product_image2']);
$product_image3 = ($_POST['product_image3']);
$product_image4 = ($_POST['product_image4']);
$product_image5 = ($_POST['product_image5']);
$sql = "INSERT INTO zain.product (product_category,product_name,product_description,product_image1,product_image2,product_image3,product_image4,product_image5)
VALUES ('$product_category','$product_name','$product_description','$product_image1','$product_image2','$product_image3','$product_image4','$product_image5')";
mysqli_query($conn,$sql);
if (mysqli_query($conn,$sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
Kindly help me how to do that
This is the simplest way:
<input type="button" name="Insert_Ad" value="Post and ad" onclick="location.href='your_url_here'">
Or you can use a form to submit to the given url:
<form action="your_url_here">
<input type="submit" name="Insert_Ad" value="Post and ad">
</form>
I would recommend using an anchor tag. If you want to give it the appearance of a button, then use CSS to style it into a button. For instance, if you were using twitter bootstrap, this would involve giving it a class of btn
<a href="/Product.php" class='btn'>Post an ad</a>
If you insist on using a button, then you might want to wrap it around a HTML form, where the action attribute points to the page you would like to navigate to. The method has to be GET. If it is the only button in the form, then it will default to being the submit button even in the absence of a type="submit" attribute.
<form action="/Product.php" method="GET">
<input type="button" name="Insert_Ad" value="Post and ad">
</form>
Related
I'm trying to create a php page where I can cycle through a database table line-by-line. I have created a page that allows me to do this but I can't seem to be able to combine the forms so that clicking the button to go to the next/previous form also submits the data entry form.
I've made a very simplified version of the code that does not have a database attached to show you what I've been doing:
<?php
session_start();
// Create an array to cycle through
$a = array('1', '2', '3', '4');
if(isset($_POST['village']))
{
$_SESSION['counter']++;
}
elseif(isset($_POST['village1']))
{
$_SESSION['counter'] = $_SESSION['counter']-1;
}
elseif(isset($_POST['testVar']))
{
$_SESSION['testVar'] = $_POST['testVar'];
}
else
{
$_SESSION['counter'] = 0;
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" http-equiv="Content Type" charset="utf-8"/>
<title>TEST</title>
</head>
<body>
<!-- Previous value form -->
<form name="prevValue_form" method="post" action="" enctype="multipart/form-data">
<div style="float: left">
<button class="btn btn-success" name="prev" id="prev" value="prev" style="font-size: 20px;"><</a>
</div>
<input type="text" name="village1" value="testing" />
</form>
<!-- Next value form -->
<form name="nextValue_form" method="post" action="" enctype="multipart/form-data">
<div style="float: left">
<button class="btn btn-primary" name="next" id="next" value="next" style="font-size: 20px;">></button>
</div>
<input type="text" name="village" value="testing" />
</form>
<!-- data input section -->
<div id="main" accept-charset="UTF-8" class="form">
<form name="dataEntry_form" method="post" action="" enctype="multipart/form-data">
<!-- data input variable -->
tester: <input name="testVar" value="" />
<!-- Values to display if code is working -->
<br clear="all" /><br />
count: <?php echo $a[$_SESSION['counter']]; ?>
<br clear="all" />
test variable: <?php echo $_SESSION['testVar']; ?>
<!-- submit the data entry form -->
<p><input name="submit" type="submit" value="Submit" /></p>
</form>
</div>
</body>
</html>
<script>
$("#nextValue_form button").click(function (ev) {
ev.preventDefault()
if ($(this).attr("value") == "next") {
$("#test_form").submit();
$("#test_form2").submit();
}
});
$("#prevValue_form button").click(function (ev) {
ev.preventDefault()
if ($(this).attr("value") == "prev") {
$("#test_form").submit();
$("#test_form1").submit();
}
});
</script>
Like I said above, the page works. However, I'm moving some people over from a bunch of Access databases to SQL Server and they don't like that the inputs don't automatically submit when they click the button to go to the next record. I'd imagine this is possible to accomplish, but I'm more of a database person than a PHP person and I've been having trouble with it.
Thanks for any help
Using this post as a guide, it might be worth it to just do one form with 2 buttons and fetch only the row you need instead of an array of rows and storing things into a session. I might only store the current id into session, but that is about it.
First I would create some basic functions to reduce duplication:
<?php
# General PDO query function. Needs to have the connection injected
function query($con, $sql, $bind = null)
{
$query = $con->prepare($sql);
$query->execute($bind);
return $query->fetch(\PDO::FETCH_ASSOC);
}
# Create a next function that uses the current id to find the next record
function toNextRecord($id, $con)
{
return query($con, 'select * from tablename where id = (select min(id) from components where id > ?)',[$id]);
}
# Use the current id to find the previous record
function toPrevRecord($id, $con)
{
return query($con, 'select * from tablename where id = (select max(id) from components where id < ?)',[$id]);
}
# Check for a post
if(!empty($_POST['action']) && $_POST['action'] == 'walk_table') {
# Fetch previous or next, depending on button press
$row = (strtolower($_POST['button']) == 'next')? toNextRecord($_POST['id'], $con) : toPrevRecord($_POST['id'], $con);
}
else
# Set the default to whatever record id to start on
$row = ['id' => 1];
?>
Make one form with an action (just makes it easier to differentiate actions) and the current id. Add the two buttons in. When clicked, only the one clicked will register in the post.
<form method="post" action="#">
<input type="hidden" name="action" value="walk_table" />
<input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
<input type="submit" name="button" value="Prev" />
<input type="submit" name="button" value="Next" />
</form>
would it be possible to have a html/php template on index.php say for example (a news webpage template and then anyone can edit the title, paragraphs only, then on submit it then sends the webpage with the data stored to a paste bin like url so who ever visits that url say http://localhost/news/jjeh3bndjks they would only be able to view to content and not edit.
I would like to use something like this
<?php
if ($_POST) {
$pasteID = uniqid();
$paste = fopen("pastes/".$pasteID.".php", "w");
$contents = $_POST['pasteContents'];
fwrite($paste, $contents);
header('Location: /pastes/'.$pasteID.'.php');
}
?>
<form action="" method="POST">
<input type="text" name="pasteContents" placeholder="write here" />
<button type="submit" tabindex="0">submit</button>
</form>
but for some reason when i add another input box or try to send anymore data it fails or just gives me the last input given is there a way to send a whole page this way?
any help would be appreciated
You can use file_get_contents with the following code:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
parse_str(file_get_contents('php://input'));
echo param1 . '<br />' . param2;
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
(You can test it here)
Although, I did success to use $_POST too:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['param1'] . '<br />' . $_POST['param2'];
} else {
?>
<form method="post">
<input type="text" name="param1" value="param1" />
<input type="text" name="param2" value="param2" />
<input type="submit" value="submit" />
</form>
<?php } ?>
Here
I have some PHP and HTML code which should send data from the form to my MySQL database. However, on clicking Submit in the form, the page reloads and nothing happens. No echo or anything. The HTML is in the same file as the PHP file.
PHP
<?php
if(isset($_POST['submit'])){
$usernamep = $_POST['usernameinput'];
$passwordp = $_POST['passwordinput'];
$servername = "localhost";
$username = "USERNAMECENSOR";
$password = "PASSWORDCENSOR";
$dbname = "database";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO accounts (username, password)
VALUES ('$usernamep', '$passwordp')";
// use exec() because no results are returned
$conn->exec($sql);
echo "Success";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
?>
HTML
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>
Note: I know this code is currently subject to SQL injection, and the password is not encrypted. It is temporary starting code in an attempt to get it working first.
You lack the name attribute in the submit button, add name="submit".
<form method="POST" action="">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" name="submit" class="button" value="Sign in">
</form>
Your test is wrong. You have no input named "submit" (with attribute name = submit). It should be:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
...
}
A broad test like this will not even need a named submit. The HTML form may even be posted on another event using jQuery or JavaScript.
In your PHP file, you use isset($_POST['submit']). You don't have any form input with name="submit". You could make your submit button be
<input type="submit" name="submit" class="button" value="Sign in">
You have to include your php file into the action of the form tag.
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="usernameinput"><br>
<input type="password" name="passwordinput"><br>
<input type="submit" class="button" value="Sign in">
</form>
I am using PHP 5.6.30 version..when I try to execute the following code data is inserted twice.i am using register.php as view and app.js for http post service.
insert.php (database connection)
<?php
$connect = mysqli_connect("localhost", "root", "","user");
$data = json_decode(file_get_contents("php://input"));
$first_name = mysqli_real_escape_string($connect, $data->firstname);
$last_name = mysqli_real_escape_string($connect, $data->lastname);
$user_name = mysqli_real_escape_string($connect, $data->username);
$emailid = mysqli_real_escape_string($connect,$data->emailid);
$password = mysqli_real_escape_string($connect,$data->password);
$mobile = mysqli_real_escape_string($connect,$data->mobile);
$query = "INSERT INTO register(fname,lname,uname,email,pass,mobile)
VALUES ('$first_name','$last_name','$user_name','$emailid','$password','$mobile')";
$result = mysqli_query($connect, $query) ;
if($result == TRUE)
{
echo "Data Inserted...";
}
else
{
echo 'Error';
}
?>
register.php:
enter code here
<html>
<head><title>Nav</title>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-
route.js"></script>
<link rel="stylesheet" type="text/css" href="css/header.css"/>
<script src="app.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="RegisterController">
<form ng-submit="rsubmit()" class="rform">REGISTER
<input type="text" class="form-control" name="firstname" ng-
model="firstname" placeholder="firstnamesss" ><br>
<input type="text" class="form-control" name="lastname" ng-
model="lastname" placeholder="lastname" ><br>
<input type="text" class="form-control" name="username" ng-model="username" placeholder="username"><br>
<input type="text" class="form-control" name="emailid" ng-model="emailid" placeholder="emailid" ><br>
<input type="text" class="form-control" name="password" ng-model="password" placeholder="password" required=""/><br>
<input type="text" class="form-control" name="mobile" ng-model="mobile" placeholder="mobileno" ><br>
<button ng-click="rsubmit()" >Register</button>
Cancel
<br></h2>
</form>
</div>
</body>
</html>
app.js:
var app = angular.module('myApp', ['ngRoute']);
app.controller('RegisterController',function($scope,$http){
$scope.rsubmit=function(){
$http.post("insert.php" ,{
'firstname':$scope.firstname,
'lastname':$scope.lastname,
'username':$scope.username,
'emailid':$scope.emailid,
'password':$scope.password,
'mobile':$scope.mobile,
} )
.success(function(data){
alert(data);
$scope.firstname = null;
// $scope.lastname = null;
});
}
});
From the documentation:
Warning: Be careful not to cause "double-submission" by using both the ngClick and ngSubmit handlers together. See the form directive documentation for a detailed discussion of when ngSubmit may be triggered.
In your code you use both ng-submit and ng-click, so you trigger the controller (the request) twice. Just remove the ngClick attribute on the button/ input field and it should work.
Just remove the ng-click from the button..Button used inside the form always submit the form..So you call the rsubmit function when click the button at same time when you click the button the form is going to submission.The form submission call again the rsubmit function..
Two types of ways for it
1.<button type="button" ng-click="rsubmit()" >Register</button> - It disable the form submission power of button when you use type="button"
(or)
2.Skip the action attribute from the form.
I created a login form that should direct the user to the page after sending the form but it just shows me a blank page
with a link of **http://localhost/Cisu/AccHolder/holderlogin.php
Login Form
<?php
session_start();
?>
<!DOCTYPE html>
<html >
<head>
<title>Login</title>
<link type="text/css" rel="stylesheet" href="css/login.css">
</head>
<body>
<form method="" action="holderlogin.php">
<div class="container">
<div class="profile">
<button class="profile__avatar" id="toggleProfile">
<img src="images/sbma.png" alt="Avatar" />
</button>
<div class="profile__form">
<div class="profile__fields">
<div class="field">
<input type="text" id="user" name="user" class="input" required pattern=.*\S.* />
<label for="username" class="label">Username</label>
</div>
<div class="field">
<input type="password" id="pass" name="pass" class="input" required pattern=.*\S.* />
<label for="password" class="label">Password</label>
</div>
<div class="profile__footer">
<input id="Submit" name="Submit "type = "Submit" class="btn"/>
</div>
</div>
</div>
</div>
</div>
<script src="javascript/login.js"></script>
</form>
</body>
</html>
holderlogin.php
<?php
ob_start();
session_start();
include('dbconn.php');// Database connection and settings
// checking the user
if(isset($_POST['Submit'])){
$user = mysqli_real_escape_string($con,$_POST['user']);
$pass = mysqli_real_escape_string($con,$_POST['pass']);
$sel_user = "select * from tbl_accholder where accholder_Username='$user' AND accholder_Password='$pass'";
$run_user = mysqli_query($con, $sel_user);
$check_user = mysqli_num_rows($run_user);
if($check_user==1){
while ($row = mysqli_fetch_array($run_user)) {
$run_ID = $row['accholder_ID'];
$run_user = $row['accholder_Username'];
}
$_SESSION['accholder_Username']= $user;
$_SESSION['accholder_ID']= $run_ID;
header( 'Location: MainMenu.php');
} else
echo "please wait while redirecting...";
echo "<script> alert('Log-In Failed!'); </script>";
echo "<script> document.location.href = 'Login.php' </script>";
ob_end_flush();
}
?>
You are checking whether $_POST['Submit'] is set.
However, in your form, the name attribute is Submit with an extra space:
<input id="Submit" name="Submit "type = "Submit" class="btn"/>
This might be the cause of the problem, since you said it shows me a blank page.
also - you have no method listed for your form, but are checking the $_POST array to check for "Submit". It should be:
<form method="POST" action="holderlogin.php">
Don't know if its causing your problem, but you have a leading space before the php declaration - this can be cause problems to the php rendering if you are setting header requests later in the code - there can be no characters rendered before the header request.
<?php
also you list the action of the form as "holderlogin.php"
but list the name of the file as "Holderlogin.php" - this might just be a typo when entering the code here but worth checking.