Submitting page with different submits buttons and forms with PHP - php

I can't figure this out alone. I have .html file and in body section I'm importing some .php files using switch. I'm displaying table and I'm navigating on it with POST form. Now I want to add to my navigation bar search input which also will send data by POST. It is located on head tag and when I press the button nothing really happens.
index.php
<html>
<head>
<div class="leftNav">
<a class="buttonNav" href="index.php?subpage=table&data=session&page=0">Session</a>
<a class="buttonNav" href="index.php?subpage=table&data=all&page=0">All</a>
</div>
<div class="rightNav">
<?php include 'navbars.php' ?>
</div>
</head>
<body style="font-family:Verdana;">
<div class="content">
<?php
if(isset($_GET['subpage']))
{
switch($_GET['subpage'])
{
case 'table':
include 'datatable.php';
break;
default:
include 'home.php';
break;
}
}
else include 'home.php';
?>
</div>
</body>
</html>
datatable.php
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['search'])
{
$filtr = $_POST['filtr'];
$data = $_GET['data'];
header("
Location: index.php?subpage=table&data=".$data."&page=0&search=".$filtr);
}
else{...}
}
//DISPLAYING TABLE//
echo("
</table>
<form class='nav' method='POST'>");
$disabled = '';
if($curr+1 == 1)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='f' value='First page' ".$disabled.">
<input type='submit' name='p' value='Previous page' ".$disabled."> ");
$disabled = "";
if($curr == $max)
$disabled = "disabled='disabled'";
echo("<input type='submit' name='n' value='Next page' ".$disabled.">
<input type='submit' name='l' value='Last page' ".$disabled."></form>");
?>
navbars.php
<?php
if(isset($_GET['subpage']) && $_GET['subpage'] == 'table')
{
echo("
<form method='POST'>
<input type='text' name='filtr'>
<input type='submit' name='search' value='Search'>
</form>
");
}
?>

As noted in the other answer, you will want to use the action attribute but you'll want to pass the $_GET['subpage'] as in:
action="<?php if(!empty($_GET['subpage'])) echo '?subpage='.htmlspecialchars($_GET['subpage'], ENT_QUOTES) ?>"
Note: I don't see anywhere that you actually send the $_GET['subpage'] or $_GET['data'] in your scripts in the first place, so I don't know if that is even being called properly.

Try Adding form action if you are referring to same page,
action="<?php echo $_SERVER["PHP_SELF"];?>"
hope it helps you.

Related

How do I pass value from Jquery to PHP ? HTML is a hidden input form field

I have a form:
<form action="post.php" method="POST">
<p class="select1">Northern Cape</p>
<p class="select1">Eastern Cape</p>
<p class="select2" >New Goods</p>
<p class="select2" >Used Goods</p>
<input id="submt" type="submit" name="submit" value="Submit">
</form>
JQUERY .... appends input/value to the selected items:
$(document).ready(function() {
$('.select1').click(function() {
if ($(this).text() == "Northern Cape"){
$(this).append("<input id='firstloc' type='hidden' name='province'
value='Northern Cape' />");
$("#firstloc").val('Northern Cape');
}; // end of if statement
if ($(this).text() == "Eastern Cape"){
$(this).append("<input id='firstloc' type='hidden' name='province'
value='Eastern Cape' />");
$("#firstloc").val('Eastern Cape');
}; // end of if statement
}); // end of click function
}); // end of document ready
$(document).ready(function() {
$('.select2').click(function() {
if ($(this).text() == "New Goods"){
$(this).append("<input id='category' type='hidden' name='cat'
value='New Goods' />");
$(this).val('New Goods');
};
if ($(this).text() == "Used Goods"){
$(this).append("<input id='category' type='hidden' name='cat'
value='Used Goods' />");
$("#category").val('Used Goods');
};
});
});
How do I pass the values to PHP ie. first value Province second value Category?
<?php
$province = $_POST['province'];
$category = $_POST['cat'];
echo $province;
echo $category;
?>
I get a message Undefined index:cat when passing to PHP.
User must select 2 items and values must be passed to PHP,I do not want to use a drop down menu with "options"
Here is the solution.
You made a several mistakes in your code.
You are using ; after if statement
You didnot close $document.ready tag properly
You have to check in your post.php if data posted or not
And you only appends input type hidden you weren't remove it.
post.php
<?php
$province = '';
$category = '';
if(isset($_POST['province'])):
$province = $_POST['province'];
endif;
if(isset($_POST['cat'])):
$category = $_POST['cat'];
endif;
echo $province;
echo $category;
?>
$(document).ready(function() {
$('.select1').click(function() {
if ($(this).text() == "Northern Cape"){
$("#firstloc").remove();
$(this).append("<input id='firstloc' type='hidden' name='province' value='Northern Cape' />");
} // end of if statement
if ($(this).text() == "Eastern Cape"){
$("#firstloc").remove();
$(this).append("<input id='firstloc' type='hidden' name='province' value='Eastern Cape' />");
} // end of if statement
});
$('.select2').click(function() {
if ($(this).text() == "New Goods"){
$("#category").remove();
$(this).append("<input id='category' type='hidden' name='cat' value='New Goods' />");
}
if ($(this).text() == "Used Goods"){
$("#category").remove();
$(this).append("<input id='category' type='hidden' name='cat' value='Used Goods' />");
}
}); // end of click function
}); // end of document ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<form action="post.php" method="POST">
<p class="select1">Northern Cape</p>
<p class="select1">Eastern Cape</p>
<p class="select2" >New Goods</p>
<p class="select2" >Used Goods</p>
<input id="submt" type="submit" name="submit" value="Submit">
</form>
</html>
Try this code hope it will helps you.
PHP does not set $_POST for all post operations, for example for text/xml:
Try adding adding application/x-www-form-urlencoded or multipart/form-data to your form just to make sure.
<form action="post.php" method="POST" enctype="multipart/form-data">
Also make sure you don't have broken HTML that is causing your form tag to be close prematurely. Verify what is getting posted and the content type using web developer tools.
You should also be using isset to check the existence of a variable:
if (isset($_POST["cat"])) {
$cat = $_POST["cat"];
echo $cat;
echo " is the category";
}
else
{
$cat = null;
echo "no category supplied";
}

add values with one another with input field submit in php calculator

I want a calculator with my PHP code that adds values with one after one with submit button.like when I input a number then submit it and show it on the page then and input other, it should add previous number.and then enter another then submit.like these, numbers are adding with one another after submitting.
<?php
session_start();
?>
<?php
error_reporting(0);
?>
<html>
<title>adding input single values</title>
<body>
<form method="post">
<input type="text" name='number' method="post"/>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number']))
{
}
else
{
$sum += $_POST['number'];
echo ++$sum;
}
?>
</body>
</html>
here is the output
You could use a hidden field instead of storing in the session.
<input type="text" name='number' method="post"/>
<?php
if(!isset($_POST['number'])) {
echo "<input type='hidden' name='prev_number' value=0 />";
} else {
$sum = $_POST['number'] + $_POST['prev_number'];
echo "<input type='hidden' name='prev_number' value=" . $sum . " />";
echo $sum;
}
?>
<input type="submit" />
</form>
<?php
if(!isset($_POST['number'])) {
// ...
}
else {
$_SESSION['number'] = isset($_SESSION['number']) ? $_SESSION['number'] : '';
$_SESSION['number'] += $_POST['number'];
echo $_SESSION['number'];
}

textarea content auto clears whenever i click the form submit button [PHP]

I have a textarea with a form submit button. Whenever I click the submit button, the content on my textarea clears. but i dont want to clear the content of my textarea. here is my code
codepage.php
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
thanks for the answers! It worked. now i have another question, what if the textarea has already a preloaded text in it and when I type in another text in it and click the submit button, the textarea should have now have the text that i inputted and the preloaded text in the textarea.
here is my updated code
<?php
$ans = "hello";
if (isset($_POST['textcode'])) {
{
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="validatePHP">
<textarea name="textcode"><?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>hell</textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>
Try render content after submit
<textarea name="textcode"><?= $_POST['textcode']; ?></textarea>
<textarea name="textcode">
<?php if(isset($_POST['textcode'])) {
echo htmlentities ($_POST['textcode']); }?>
</textarea>
Maybe you could do it with $_SESSION?
at the top of your page type session_start();
then inside
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
}
add the code $_SESSION['textareaMsg'] = $_POST['textcode']; like this...
if ($_POST['textcode'] == $ans) {
echo "<div id=errorPlace>proceed to next lesson</div>";
$_SESSION['textareaMsg'] = $_POST['textcode'];
}
then where your text area is set just replace it with this.
<?php
if(isset($_SESSION['textareaMsg'])){
echo '<textarea name="textcode">'.$_SESSION['textareaMsg'].'</textarea>';
}else{
echo '<textarea name="textcode"></textarea>';
}
?>
This works by saving the text area as a session variable when you submit the form, and checking if its set when you load the form, if it is then it will replace the contents of the text area with what is set in the session. Hope this helps!
Try following code
<?php
$ans = "hello";
$textcode = ""; //declare a variable without value to avoid undefined error
if (isset($_POST['textcode'])) {
{
$textcode=$_POST['textcode']; //assign the value to variable in you if statment
if ($textcode == $ans) { //useing variable in if statment
echo "<div id=errorPlace>proceed to next lesson</div>";
}
else
{
echo "<div id=errorPlace>Error</div>";
}
}
}
?>
<form method="POST" name="test.php">
<!--echo user input -->
<textarea name="textcode"><?php echo $textcode; ?></textarea>
<input type="submit" class="btnSubmit" title="Submit Code" name="add" value=""></input>
</form>

always hiding a button after clicking on it using php and css

I have this php code which hide the download button after clicking on it one time by changing the ID from 0 to 1 .. after that if another time the user signed in , it removes the button using a simple css hide code.
here is my code :
<?php
$result = #mysql_query("SELECT * FROM scode WHERE updated= 1 and coden ='$username'");
if ($_POST[downloadTheFile]== "downloadTheFile")
{
$upd_art = "update scode set downloaded='".$_POST[t11] ."' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
if($row['downloaded']==1)
{
echo "<style>
.thedownloadbutton {display:none;}
</style>";
}
?>
<form class="thedownloadbutton" method="get" action="<? echo '../download/'.$item_downloadlink .'.zip' ; ?>">
<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>
<input name="t11" type="hidden" size="2" value="1">
</form>
(just for clarifying: updated =1 is a field that'll open the download page.. if the updated =1 then there is a download page )
I don't know why it doesn't work ..
can you help me please and till me which part is the wrong part ?
I know it's a bad way to hide an element using css , is there another suggestion ?
$_POST[downloadTheFile] should be $_POST['downloadTheFile']
if($row['downloaded']!=1)
{
echo '<button type="submit" name="downloadTheFile" value="downloadTheFile">Download </button>';
}
so there were some problems in this code .. it wasn't professional at all so I used this new code which has a button that update the number from 0 to 1 on the database
(that will tell the download button to not show after that)
then shows the download button
<?
$res = mysql_query("SELECT downloaded FROM scode WHERE id='$_SESSION[userid]'");
$row = mysql_fetch_array($res);
// echo $row['downloaded'];
echo '<br>';
?>
<form method="POST" action=''>
<?
if($row['downloaded'] ==='0')
{
echo "<input type='submit' name='button1' value='click to show the download link' onclick ='validatea(); return false;' />";
//echo '<button type="submit" formmethod="post" name="downloadTheFile" value="downloadTheFile">Download </button>';
?>
</form>
<?
if (isset($_POST['button1']))
{
?> <form class="aaa" method="POST" action="<? echo "../download/".$item_downloadlink .".zip" ; ?>">
<? ;
?> <input type='submit' name='submit' value='download' onclick ='validate(); return false;' /> <? ;
echo"</form>";
$upd_art = "update scode set downloaded='1' where id='$_SESSION[userid]'";
mysql_query($upd_art) or die(mysql_error());
}
}
else {
echo "you already downloaded the file , if you have any problem please contact us";
echo"<br>";
}
?>

form disappears when it is submitted in PHP [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I've got the following in my code and I cant get to work the latter part of the code work. To be more precise on my question, this works the way I want up until I click OneClick. When that's done I get another form (form2) with a TwoClick submit button dynamically populated. The issue comes here. When I click the button TwoClick the whole 'form2' disappears. can some one throw me a clue as to where I have gone wrong?
Thanks.
<?php session_start(); ?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
?>
</div>
</body>
</html>
try this
<?php
session_start();
function putForm2(){
$myForm = "<form name='form2' method='post' action='#'><table border='1'><tr><td>form 2 is here!<br></td></tr><tr><td><input type='text' name='txt123' id='txt123'></td></tr> <tr><td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td></tr></table></form>";
return $myForm;
}
?><!DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="#">
<?php echo "<input type='text' name='txt1' id='txt1'>"; // This text box is dynamically populated ?>
<input type="submit" name="sendone" id="sendone" value="OneClick">
</form>
</div>
<div id="two">
<?php
if(isset($_POST['sendone']))
{ if($_POST['txt1'] == '')
{echo 'txt1 is empty!'; return;} else {$_SESSION['txt1'] = $_POST['txt1'];}
if(isset($_SESSION['txt1']))
echo $_SESSION['txt1'];
echo putForm2();
}
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{
echo putForm2();
echo "Text box is empty..."; return;
}
}
?>
</div>
</body>
</html>
Session_start() must be called before outputting any output (in the beggining of the script).
Sendtwo form disappears because of your logic - when you submit Sendtwo, $_POST['sendone'] is not set, therefore not being echoed. To fix that you could for example change the first condition to:
if (isset($_POST['sendone']) || isset($_POST['sendtwo']))
try putting
if(isset($_POST['sendtwo']))
if(isset($_POST['sendtwo']))
{
if($_POST['txt123'] == '')
{echo "Text box is empty..."; return;}
}
at the very end. i mean after the last }
Try this example and read the code comments. Though really its unclear to me what your trying todo so ive just ported code to what I think your trying todo, tho there are better ways handle processing forms. Also You should really think about your form value keys, txt1 and txt123 are not helpful and dont explain the type of variable you want from the form. Perhaps its of interest.
<?php
session_start();
if($_SERVER['REQUEST_METHOD']=='POST'){
$form = null;
//Handle form 1
if(isset($_POST['sendone'])){
//reset session vars if new request
unset($_SESSION['txt1']);
unset($_SESSION['txt123']);
//validate txt1
if(!empty($_POST['txt1'])){
//set into session
$_SESSION['txt1'] = $_POST['txt1'];
//or you could put the value in a <input type="hidden" name="txt1" value="'.htmlspecialchars($_POST['txt1']).'"/>
$form = "
<form name='form2' method='post' action=''>
<table border='1'>
<tr>
<td>form 2 is here!<br></td>
</tr>
<tr>
<td><input type='text' name='txt123' id='txt123'></td>
</tr>
<tr>
<td><input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'></td>
</tr>
</table>
</form>";
} else {
$error['txt1'] = 'txt1 is empty!';
}
}
//do second form
if(isset($_POST['sendtwo'])){
//validate
if(!empty($_POST['txt123'])){
//set session
$_SESSION['txt123'] = $_POST['txt123'];
}else{
$error['txt123'] = 'txt2 is empty!';
}
}
//check then do something with both form values
if(empty($error) && isset($_SESSION['txt1']) && isset($_SESSION['txt123'])){
$form = "Both txt1=".htmlspecialchars($_SESSION['txt1'])." and txt123=".htmlspecialchars($_SESSION['txt123'])." was set into session";
}
}
?>
<DOCTYPE html>
<html>
<head><title></title>
</head>
<body>
<div id="One">
<form name="form1" method="post" action="">
<input type='text' name='txt1' id='txt1'>
<input type="submit" name="sendone" id="sendone" value="OneClick">
<?php echo isset($error['txt1'])?$error['txt1']:null;?>
</form>
</div>
<div id="two">
<?php echo isset($form)?$form:null;?>
</div>
</body>
</html>

Categories