On basis of the information that the user fills in my form, I want to execute some PHP code. So after the form is submitted, it should have control on the same page and thereafter it should execute the PHP code (and not before pressing submit button). I have used <input type="hidden" value=1 name="hid"/>. When the user clicks the submit button, the value is changed to 0. But its not working. so solution please..
Is this similar to what you are looking for ?
<?php
if (!isset($_POST["submit"]) ) {
if ($_POST["hid"] == 0 ) {
echo "hid is not 0. display form.";
}
?>
<html>
<head>
<script type="text/javascript">
function check_valid() {
document.getElementById("hid").value = 0;
}
</script>
</head>
<body>
<form method="POST" action="<?php echo $PHP_SELF;?>" onsubmit="return check_valid();" >
<input type="hidden" id="hid" name="hid" value="1" />
<input type="submit" value="submit" name="submit"/>
<!-- form elements go here -->
</form>
</body>
</html>
<?php
} else {
echo "hid is now 0, execute the php code";
}
?>
EDIT: added <input type="hidden" name="hid" value="1" /> for clarity. Thanks to andre_roesti for the suggestion
Related
im currently trying to get a value from a textbox but im always getting undefined array key.
<head>
<!-- Links -->
<link rel="stylesheet" href="./css/vouches.css">
</head>
<body>
<div class="content">
<form method="get"><input type="text" id="oderid" name="oderid" placeholder="oderid / invoiceid"></form>
<form method="post"><input type="submit" name="button1"class="submitbtn" value="Button1" /></form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_GET['orderid'];
echo $id;
}
?>
</div>
</body>
</html>
in the part is where the stuff happens. I hope i can get some help, im really new to php
When clicking the button it should echo $id, better said the textbox input
Don't use both POST and GET; pick one.. Like this:
<form method="post">
<input type="text" id="oderid" name="orderid" placeholder="oderid / invoiceid" />
<input type="submit" name="button1" class="submitbtn" value="Button1" />
</form>
<?php
if(array_key_exists('button1', $_POST)) {
button1();
}
function button1() {
$id = $_POST['orderid'];
echo $id;
}
?>
Also take care of your spelling, orderid is not the same as oderid.
I updated the question.
Since the last code was pretty complex and even after fixing the stuff it didn't work, I executed the below simple code to check if things work. Even this code doesn't work. Whenever I click on the submit button, it again returns a 404 error.
Yes, I placed the PHP code in the body as well to check if this work but it doesn't.
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<html>
<head>
<title>Echo results!</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
</body>
</html>
Try giving the button_create as name of the submit button
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
if(isset($_POST['button_create'])) {
<td><input type="submit" name="button_create" id="button_create" value="Create Table!"></td>
change these lines see how you go from there
There are a couple of things wrong here, method should be POST instead of GET. The name attribute of text fields should be used when receiving the values. The submit button name should be used to check whether the button is clicked or not. See the example given below.
<?php
if (isset($_POST['submit'])) {
$ex1 = $_POST['ex1'];
$ex2 = $_POST['ex2'];
echo $ex1 . " " . $ex2;
}
?>
<form action="" method="post">
Ex1 value: <input name="ex1" type="text" />
Ex2 value: <input name="ex2" type="text" />
<input name="submit" type="submit" />
</form>
Echo results!
<?php
if(isset($_POST['submit'])) {
echo("Done!!!!");
} else {
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input name="submit" type="submit" value="submit"/>
</form>
<?php
}
?>
this is for your updated question
I have this code :
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="selectForm" id="selectForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<input type="checkbox" name="checkbox" form="selectForm" />CheckBox
</form>
<input type="button" value="Submit" name="submit" onclick="document.selectForm.submit();" />
<?php
if(isset($_POST['submit'])) {
echo "Submitted";
} else {
echo "Not submitted";
}
?>
</body>
</html>
I can not detect if the form was submitted. I would like to modify the PHP code and not the html, if possible.
Previously I had my Submit type input inside the form and it worked, but now that it is outside and I use JavaScript to do the submit it does not work.
How can I detect if the form is submitted ?
The $_POST['submit'] will no longer exist, since the submit button is no longer part of the form. Instead we can check the $_SERVER['REQUEST_METHOD'].
if($_SERVER['REQUEST_METHOD'] === 'POST') {
echo 'Submitted';
}
Also, if you leave the form's action attribute blank, it will submit to the current page:
<form name="selectForm" id="selectForm" action="" method="post">
Update:
Add a hidden field with the name of your form:
<input type="hidden" name="formname" value="selectForm" />
<?php
if(isset($_POST['formname'])) {
echo $_POST['formname'] . ' submitted';
}
?>
i have a simple website which is written by php and mysql code. i have a detect button on my my sql table query page and given below code is writen for this function but my problem is i need a popup window when the detect link is clicked. i have tired to set a code in my created code but i am not able .kindly please help me solve this problem.
<?php $sezione="home_admin"; if(isset($_POST['messaggio']))
$messaggio=$_POST['messaggio'];
include("control_admin..php");
$canc_id=$_GET['canc_id'];
$idcorsocanc=$_POST['idcorsocanc'];
$action=$_REQUEST['action'];?>
<?php
/*echo "permessi".$permessi;
echo "<br>id".$id_nome;*/
if($action=='canc'){?>
<h1>are you sure want to delect the course?</h1>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="canc1" style="float: left; margin-left:25px;">
<input type="hidden" name="idcorsocanc" value="<?=$canc_id?>">
<input type="hidden" name="action" value="">
<input type="submit" name="ok" value="Si,cancella" class="puls_invia">
</form>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" name="canc2" style="float: left; margin-left:25px;">
<input type="hidden" name="action" value="">
<input type="submit" name="ok" value="NO" class="puls_invia">
</form>
<?php
}
ok i want to update my question cause i follow one answer and here the code is-
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$('#ok').click(function(){
if(confirm('Are you sure ?')){
$('#form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<?php
if(isset($_POST['action'])){
if($_POST['action'] == 'deleted'){
//the form has been sent, do something
}
}else{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="form">
<input type="button" id="ok" name="ok" value="Delete">
<input type="hidden" id="action" name="action" value="deleted">
</form>
<? } ?>
</body>
</html>
but till now my problem is i alreday have link name delect and if i click that link i saw another delete button cause now i use the following code which i just update then if i click there i saw the pop up window but if i click ok that course is not delete cause i guess something is missing.
my actual need is i alreday have delect link and i need something that if i click on that i saw one opoup window.just this is my need.
You need a client-side script to manage this. I'd recommend something in jQuery.
<script type="text/javascript">
$(document).ready(function(){
$(".myButton").click(triggerPopup);
})
function triggerPopup(){
//do popup stuff
}
</script>
an example in more details can be found by googling. something like this http://istockphp.com/jquery/creating-popup-div-with-jquery/
You should do this in javascript. Especially with jquery library
This should look like this :
<?php
include("control_admin.php");
$sezione = "home_admin";
$canc_id = $_GET['canc_id']; //i'm gessing this is the ID to delete ?
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
$('#ok').click(function(){
if(confirm('Are you sure ?')){
$('#form').submit();
}else{
return false;
}
});
});
</script>
</head>
<body>
<?
if(isset($_POST['action'])){
if($_POST['action'] == 'deleted'){
$id = $_POST['id'];
$sql = "delete from table_name where column_id = ".$id;
mysql_query($sql);
echo $canc_id . ' has been deleted!';
}
}else{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" id="form">
<input type="button" id="ok" name="ok" value="Delete">
<input type="hidden" id="action" name="action" value="deleted">
<input type="hidden" id="id" name="id" value="<?=$canc_id?>">
</form>
<? } ?>
</body>
</html>
I'm doing php that is textbox a value empty it will open a alertbox (I'm using javascript in here )
this is my code
<?php
include('config.php');
if(isset($_POST['submit'])){
$username=$_POST['username'];
?>
<script>
function validate(){
if(document.forms[0].username.value==""){
window.alert("You must enter both values");
return false;
}
}
</script>
<?php
}
?>
<html>
<div><p>Member Profile</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username<br>
<input class="user" type="text" name="username" id="username" /><br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</html>
The problem is i have to click 2 times before the alert show
please help me to solve this problem
It's because the script is inside the php if(isset){} block, one click submits the form, which generates the script and then it works the second time.. try this setup instead:
<?php
include ('config.php');
if (isset($_POST['submit']))
{
$username = $_POST['username'];
}
?>
<html>
<head>
<script>
function validate () {
if (document.forms[0].username.value == "") {
window.alert("You must enter both values");
return false;
}
}
</script>
</head>
<body>
<div>
<p>
Member Profile
</p>
<form action="testing.php" method="POST" onsubmit="return validate();">
Username
<br>
<input class="user" type="text" name="username" id="username" />
<br>
<input type="submit" name="submit" value="register" />
</form>
</div>
</body>
</html>
Edit:
I've moved the script tag inside the head tag. I'm not sure if there are any implications for having the script outside but just to be sure I've moved it.
2nd Edit: (OCD is kicking in)
I've added body tags, not sure if you copied and pasted this code but it looked weird to me :)