How to send POST request using html button - php

I want to send post request using html button. I know it is easily done by jQuery ajax, but i do not want to use jquery and ajax. How can i do it?
This my function
public function editProduct() {
if($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'You are authorized';
} else {
echo 'You are not authorized to access this page.';
}
}
This is my HTML button
<button type="submit" onclick="location.href = '<?=base_url().'company/admin/add_product/editProduct?>';">Send Post Request</button>

In your form tag, just write a method="post"
<form method="post">
...
<button type="submit" >
</form>

You have two ways to do this:
via form (best method):
<form action ="<?php echo base_url().'company/admin/add_product/editProduct'?>" method="post">
<input type="submit" value="Send Post Request">
</form>
via javascript (jquery: http://api.jquery.com/jquery.post/ )

Related

window.print function call after form validating form in php

I have a problem, i want to print a a div after form validating and then submitting. Issue is, when call print.window function in submit button, it display print window without validating form fields.
input type="submit" value="Submit" class="button" name="submit" onclick="window.print()">
while the div which to be print is in
if($_POST['submit'])
{
echo"print div here";
}
Please help.
Thanks
If you want to print something after form validating and then submitting, you are supposed to do it in client side.
You can do something like this:
<form name="myForm" action="action.php" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
Then, validate form like this and print.
<script>
function validateForm() {
// validate your form here
window.print();
}
</script>
Hope it helps.

Form Submit Button Works, but not Submit() in Link

I've done this so often before on different websites, but can't get it to work now.
I've got a simple form that posts perfectly well using a submit button, but for a specific reason I actually need it to submit via a url link instead. I'm using submit(). The form submits, but the data isn't posting.
What am I missing?
<html>
<body>
<?
if(isset($_POST['bar'])) { echo 'testing button<br>'; }
if(isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) { oForm.submit(); }
else { alert("DEBUG - could not find element " + formId); }
}
</script>
</body>
</html>
The form starts to submit, then the href of the link is followed, and this cancels the form submission.
If you are using old-style onclick attributes, then return false; at the end to prevent the default action.
You would, however, be better off using a submit button (you are submitting a form). You can use CSS to change its appearance.
Try this code :
<html>
<body>
<?php
if (isset($_POST['bar'])) {
echo 'testing button<br>';
}
if (isset($_POST['information'])) {
echo $_POST['information'];
echo '</br>Info successfully posted.';
}
?>
<form action="test.php" method="post" id="fooform">
Hello World.<br>
Select checkbox: <input type="checkbox" id="information" name="information" value="yes">
<input type="submit" name="bar" value="Send"><br>
Confirm and Post<br>
Post Directly
</form>
<script type="text/javascript">
function SubmitForm(formId) {
var oForm = document.getElementById(formId);
alert("Submitting");
if (oForm) {
oForm.submit();
}
else {
alert("DEBUG - could not find element " + formId);
}
}
</script>
</body>
</html>
try to submit form with form id in jquery
<a class="submit">Post Directly </a>
$('a.submit').click(function(){
$('#fooform').submit();
})

submit data via URL bar in PHP and HTML

I have this very basic form in my html page.
<form action="post.php" method="post">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and then stores the data onto my database backend.
id also want to submit data via URL bar, such as this.
http://localhost/test.php?message=test&submit=send
but when i try to do above, nothing happens.
how can i achieve such method?
[EDIT]
my post.php
<?php
include_once("connect.php");
if (isset($_GET['submit'])) {
if ($_GET['message'] == "") {
echo " no input, return";
exit();
}
else {
$message = $_GET['message'];
mysql_query("insert into data (message) values ('$message')");
header ('location:index.php');
exit ();
}
}
else {
echo "invalid";
}
?>
use GET method instead of POST
so your code should be like follow:
<form action="post.php" method="GET">
Message: <input type="text" name="message" />
<input type="submit" name="submit" value="send">
</form>
and in the post.php you can get those Query string by using $_GET['message'] or $_REQUEST['message']
use a form GET method. to submit data of a form as a query string.
<form action="test.php" method="GET">

Passing HTML Input Value To The Form Action in a New Window Using Javascript

Here is my javascript and php code.
js
function validate()
{
myform.submit();
}
php
<form action="http://myurl/" method="post" name="myform" target="_blank">
<input type="id" name="txtid" value="3">
<input type="button" value="Submit" onClick="validate()">
</form>
How can I pass the value of txtid to the form action so that my new window URL would have http://myurl/3. Any idea how to trick this?
Thank you..
Try this
function validate()
{
//grabs the value of txtid
txtid = $('#txtid').val();
//uses var to redirect to the correct URL
window.open(http://myurl/"+txtid);
}

Calling a particular PHP function on form submit

I was trying to call a particular php function in submit of a form both the form and php scripts are in same page. My code is below.(it is not working and so I need help)
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
</body>
</html>
In the following line
<form method="post" action="display()">
the action should be the name of your script and you should call the function, Something like this
<form method="post" action="yourFileName.php">
<input type="text" name="studentname">
<input type="submit" value="click" name="submit"> <!-- assign a name for the button -->
</form>
<?php
function display()
{
echo "hello ".$_POST["studentname"];
}
if(isset($_POST['submit']))
{
display();
}
?>
you don't need this code
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
?>
Instead, you can check whether the form is submitted by checking the post variables using isset.
here goes the code
if(isset($_POST)){
echo "hello ".$_POST['studentname'];
}
click here for the php manual for isset
Assuming that your script is named x.php, try this
<?php
function display($s) {
echo $s;
}
?>
<html>
<body>
<form method="post" action="x.php">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
PHP is run on a server, Your browser is a client. Once the server sends all the info to the client, nothing can be done on the server until another request is made.
To make another request without refreshing the page you are going to have to look into ajax. Look into jQuery as it makes ajax requests easy
If you want to call a function on clicking of submit button then you have
to use ajax or jquery,if you want to call your php function after submission of form
you can do that as :
<html>
<body>
<form method="post" action="display()">
<input type="text" name="studentname">
<input type="submit" value="click">
</form>
<?php
function display()
{
echo "hello".$_POST["studentname"];
}
if($_SERVER['REQUEST_METHOD']=='POST')
{
display();
}
?>
</body>
</html>
Write this code
<?php
if(isset($_POST['submit'])){
echo 'Hello World';
}
?>
<html>
<body>
<form method="post">
<input type="text" name="studentname">
<input type="submit" name="submit" value="click">
</form>
</body>
</html>
An alternative, and perhaps a not so good procedural coding one, is to send the "function name" to a script that then executes the function. For instance, with a login form, there is typically the login, forgotusername, forgotpassword, signin activities that are presented on the form as buttons or anchors. All of these can be directed to/as, say,
weblogin.php?function=login
weblogin.php?function=forgotusername
weblogin.php?function=forgotpassword
weblogin.php?function=signin
And then a switch statement on the receiving page does any prep work and then dispatches or runs the (next) specified function.

Categories