stuck with a simple php code - php

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.

Related

How to PHP post back [duplicate]

This question already has answers here:
How do I get PHP errors to display?
(27 answers)
Closed 2 years ago.
attempting to implement PHP post back to my form. I'm unsure on how to do this properly and any pointers would be greatly appreciated as to remove errors. As you can tell, I'm an amateur! I understand that the line if (isset($_REQUEST["submit-button"])) needs to be implemented, where exactly, I've been directed to the top. Any recommendations or answers to make my code better is greatly appreciated. Thank you in advance!
<?php
if ( isset($_POST["submit-button"]) && $_POST["submit-button"]=="submit") {
$fname = preg_replace("/[^a-zA-Z]/", "", $_POST['firstname']);
$email = preg_replace("/[^a-zA-Z0-9\/:#\.\+-s]/", "", $_POST['email']);
$address = preg_replace("/[^a-zA-Z0-9 \n\r#.]/", "", $_POST['postaddr']);
$sport = preg_replace("/[^a-zA-Z0-9]/", "", $_POST['favsport']);
$elist = preg_replace("/[^a-zA-Z0-9\/:#\.\,+-s]/","", $_POST['emaillist']);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Week 7 Exercise 3 Form</title>
<style>
:root{
--width: 600px;
--label: 200px;
}
*{
font-size:16px;
font-family:Arial,sans-serif;
}
h1 {
color: red;
font-size: 2.4em;
}
p {
/* display: table-row; */
font-size: 1em;
color: blue;
width: VAR(--width);
border: 1px solid #eee;
background-color: #eee;
padding: 3px;
}
label {
vertical-align: top;
display: inline-block;
padding: 3px;
width: VAR(--label);
}
select,
input,
textarea {
position: relative;
width: CALC(VAR(--width) - VAR(--label) - 20px);
border: 1px solid #eee;
padding: 3px;
right: 0px;
}
select{
width: 8em;
}
input#list{
width: 1em;
}
input#submit{
position: relative;
/* display: inline-block; */
width: 5em;
margin-left:auto;
margin-right:0;
border: 1px solid #000;
}
</style>
</head>
<body>
<h1>Week 7 Exercise 3 PHP form demo</h1>
<form id="userinfo" action="<?=htmlentities($_SERVER['PHP_SELF']);?>" method="post" enctype="application/x-www-form-urlencoded">
<p>
Please fill in the following form. All fields are mandatory.
</p>
<p>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="firstname" value=<?=(($fname)?$fname:"")?>>
</p>
<p>
<label for="email">Email Address:</label>
<input type="text" id="email" name="email" value=<?=(($email)?$email:"")?>>
</p>
<p>
<label for="addr">Postal Address:</label>
<textarea id="addr" name="postaddr"><?=(($address)?$address:"")?></textarea>
</p>
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport[]" size="4" multiple="multiple">
<option value="soccer" <?=((in_array("soccer",$sport))?'selected':'')?>>Soccer</option>
<option value="cricket" <?=((in_array("cricket",$sport))?'selected':'')?>>Cricket</option>
<option value="squash" <?=((in_array("squash",$sport))?'selected':'')?>>Squash</option>
<option value="golf" <?=((in_array("golf",$sport))?'selected':'')?>>Golf</option>
<option value="tennis" <?=((in_array("tennis",$sport))?'selected':'')?>>Tennis</option>
<option value="basketball" <?=((in_array("basketball",$sport))?'selected':'')?>>Basketball</option>
<option value="baseball" <?=((in_array("baseball",$sport))?'selected':'')?>>Baseball</option>
</select>
</p>
<p>
<label for="list">Add me to the mailing list</label>
<input type="checkbox" id="list" name="emaillist" value="Yes" <?=(($elist)?' checked ':"")?>>
</p>
<p>
<input type="submit" id="submit" value="submit" name="submit-button">
</p>
</form>
<section id="output">
<h2>The following information was received from the form:</h2>
<p><strong>First Name:</strong> <?=$fname;?></p>
<p><strong>Email Address:</strong> <?=$email;?></p>
<p><strong>Postal Address:</strong> <?=$address;?></p>
<p><strong>Favourite sport = </strong> <?=ucwords(implode(", ",$sport));?></p>
<p><strong>Email list = </strong> <?=$elist;?></p>
</section>
</body>
</html>
Write your code like below:
<?php
$fname=""; $email=""; $address=""; $sport=""; $elist="";
if (isset($_POST['submit-button'])) {
$fname = $_POST['firstname'];
$email = $_POST['email'];
$address = $_POST['postaddr'];
$sport = $_POST['favsport'];
$elist = $_POST['emaillist'];
}
?>
Keeping your existing HTML the same, you can modify the PHP snippet like so in order to get your posted data:
<?php
if ( $_POST ) {
$fname = $_POST['firstname'];
$email = $_POST['email'];
$address = $_POST['postaddr'];
$sport = $_POST['favsport'];
$elist = $_POST['emaillist'];
}
?>
This simply checks to see if anything has been posted to the page, ie. the $_POST array is not empty.

Html form into a Php table

So, im trying to get the informatino you put into an HTML into a Table in Php, problem is, i cant find any solutions online. and i dont know much about php yet. this is only my second time asking something so i still dont quite know the way you are supposed to fill in a question. anyway, here is the code in question:
HTML
</head>
<body>
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
<div>
<label for="vnaam">Voornaam</label>
<input type="text" id="vnaam" name="vnaam" required/>
</div>
<div>
<label for="anaam">Achternaam</label>
<input type="text" id="anaam" name="anaam" required/>
</div>
<div>
<label for="tnum">Telefoon Nummer</label>
<input type="text" id="tnum" name="tnum" />
</div>
<div>
<label for="tnum">E-mail</label>
<input type="email" id="tnum" name="tnum" />
</div>
<div>
<label for="adres">Adres</label>
<input type="" id="adres" name="adres" required/>
</div>
<div>
<label for="land">Land</label>
<select id="land" name="land">
<option value="ned">Nederland</option>
<option value="usa">Amerika</option>
<option value="eng">Engeland</option>
<option value="bel">Belgiƫ</option>
<option value="fr">Frankrijk</option>
<option value="ger">Duitsland</option>
</select>
</div>
<div class="button">
<button type="submit">Opsturen</button>
</div>
</form>
</body>
</html>
Css
<style type="text/css">
form {
/* Just to center the form on the page */
margin: 0 auto;
width: 400px;
/* To see the outline of the form */
padding: 1em;
border: 1px solid #CCC;
border-radius: 1em;
}
form div + div {
margin-top: 1em;
}
label {
/* To make sure that all labels have the same size and are properly aligned */
display: inline-block;
width: 90px;
text-align: right;
}
input, textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
/* To give the same size to all text field */
width: 300px;
-moz-box-sizing: border-box;
box-sizing: border-box;
/* To harmonize the look & feel of text field border */
border: 1px solid #999;
}
input:focus, textarea:focus {
/* To give a little highlight on active elements */
border-color: #000;
}
textarea {
/* To properly align multiline text fields with their labels */
vertical-align: top;
/* To give enough room to type some text */
height: 5em;
/* To allow users to resize any textarea vertically
It does not work on all browsers */
resize: vertical;
}
.button {
/* To position the buttons to the same position of the text fields */
padding-left: 90px; /* same size as the label elements */
}
button {
/* This extra margin represent roughly the same space as the space
between the labels and their text fields */
margin-left: .5em;
}
</style>
<form method='POST' action='formhandler.php'>
<input type='text' name='name' value='john'>
<input type='text' name='address' value='32 flowers street'>
<input type='text' name='email' value='john#example.com'>
</form>
A typical form like this one will be delivered to the php file formhandler.php by the browser.
Then you handle it in your formhandler.php file
formhandler.php
$name = isset($_POST['name']) ? $_POST['name'] : "";
$address= isset($_POST['address']) ? $_POST['address'] : "";
$email= isset($_POST['email']) ? $_POST['email'] : "";
echo $name; //john
echo $address; //32 flowers street
echo $email; //john#example.com
Now you have the form data. use it as you like, store them in a database, build another html response file to the user or do what ever you want to do with them.
for example, we are going to feedback the data to the user.
echo "We have successfully received your inputs as <br>
<table>
<tbody>
<tr><td>name</td><td>$name</td></tr>
<tr><td>address</td><td>$address</td></tr>
<tr><td>email</td><td>$email</td></tr>
</tbody>
</table>";
Note: in a real life production you need to validate any inputs come to your php files first.
First change
<form action="http://www.cs.tut.fi/cgi-bin/run~jkorpela/echo.cgi"method="post">
to Something like
<form action="http://www.cs.tut.fi/getdata.php" method="post">
Because you are using cgi-bin/run~jkorpela/echo.cgi which is not used for get php data because its a cgi file. it should a .php extension file and php should be installed on your server. and I am assuming that http://www.cs.tut.fi/ is the url for your website.
Where getdata.php is the php file where you get all your input values. You need to create that file on your server. If you are on AWS then it might be something like /var/www/html/
getdata.php
<?php
echo "<pre>";
print_r($_REQUEST);
?>

How do i display errors and keep DIV ( specialbox) on page upon submission

I wanna make it in a way that after submitting the form. The form reverts back to the div special box containing the form , stating alll the errors. This means that I dont have to click the button to reopen to see the errors in the form. Any ways to work around my code? Greatly appreciated!!!!!
CSS:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
div#overlay {
display: none;
z-index: 2;
background: #000;
position: fixed;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
text-align: center;
}
div#specialBox {
display: none;
position: relative;
z-index: 3;
p.padding;
padding-top:25px;
padding-bottom:25px;
padding-right:50px;
padding-left:50px;
margin: 150px auto 0px auto;
width: 500px;
height: 500px;
overflow:auto;
background: #FFF;
color: #000;
}
div#wrapper {
position:absolute;
top: 0px;
left: 0px;
padding-left:24px;
}
</style>
<script type="text/javascript">
function toggleOverlay(){
var overlay = document.getElementById('overlay');
var specialBox = document.getElementById('specialBox');
overlay.style.opacity = .8;
if(overlay.style.display == "block"){
overlay.style.display = "none";
specialBox.style.display = "none";
} else {
overlay.style.display = "block";
specialBox.style.display = "block";
}
}
</script>
</head>
<body>
<!-- Start Overlay -->
<div id="overlay"></div>
<!-- End Overlay -->
<!-- Start Special Centered Box -->
<div id="specialBox">
<p>Create Order
<p><?php
$timestamp=time(); require_once 'start.php';
?>
HTML:
<form action="tradeform.php" method="post" >
<input type="hidden" name="formSubmitted" value="true">
<?php echo $message; ?>
<?php ?>
<?php if ($haveErrors || $userArriveByClickingOrDirectlyTypeURL) : ?>
<fieldset>
<p>Symbol : <select name = "selection" id="selection">
<option disabled = "disabled" selected = "selected"> Choose one </option>
<option value="eur/usd">EUR/USD</option>
<option value="usd/jpy">USD/JPY</option>
<option value="usd/cad">USD/CAD</option>
<option value="eur/jpy">EUR/JPY</option>
<option value="eur/chf">EUR/CHF</option>
<option value="gbp/usd">GBP/USD</option>
<option value="aud/usd">AUD/USD</option>
<option value="usd/chf">USD/CHF</option>
</select><font color="red"><?php echo $selectionError?></font></p>
</fieldset> <fieldset>
<p> Date : <input type="datetime" value="<?php echo date("Y-m-d ",$timestamp); ?>"READONLY name="date"/></p>
<p> Type : <input type="radio" name="type" value="buy">Buy <input type="radio" name="type" value="sell">Sell<font color="red"><?php echo $typeError;?></font></p>
<p> Size : <input type="number"pattern="[0-9]+([\.|,][0-9]+)?" step="0.01"name="size"/><font color="red"><?php echo $sizeError?></font></p>
<p> Bid Price : <input id="bidprice" READONLY name="bidprice" type="text" value=""> <font color="red"><?php echo $bidpriceError?></font></p>
<p>Offer Price<input id="offerprice" READONLY name="offerprice" type="text" value=""><font color="red"><?php echo $offerpriceError?></font> </p>
<p> Stop Loss : <input type="number"step="any" name="stoploss"/><font color="red"><?php echo $stoplossError?></font></p>
<p> Take Profit : <input type="number"step="any"name="takeprofit"/><font color="red"><?php echo $takeprofitError?></font></p>
</fieldset>
<div align="center">
<input type="submit" value="Submit"/>
</div>
<?php endif; ?>
</form>
I'd suggest using JQuery for this
$('input[type=submit]').click(function(e) {
e.preventDefault();
$.post("tradeform.php", $(this).parents('form').serialize(), function(response) {
*INSERT RESPONSE HANDLER HERE*
});
});
Obviously it's cleaner to replace the input type submit by a button to avoid having to preventDefault().
Your handler could show error messages, hide the form or do what ever else you'd want it to. Since your form is relatively straight-forward, have you considered using error messages built-in to HTML5 forms?
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms_in_HTML

sql query on button click

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>

Drop down menu WordPress widget

<form id="promacform" onsubmit="return protein_calculator(this);" method="post">
Weight: <input type="text" name="protein_weight" id="protein_weight" size="5"; /> lbs.<br />
Activity level (1.1-1.4): <input type="text" name="protein_activity" id="protein_activity" size="5"; /> <br />
<br><input type="submit" name="submit" id="submit" value="Calculate" /><br />
<div id="protein_result"></div>
</form>
Im trying to add a drop down menu for the activity level which will have 4 options:
Lightly active (moderate exercise but sedentary job)
Moderately active (intense exercise but sedentary job)
Very active (moderate exercise and active job)
Extra active (intense exercise and active job)
im not quite sure how to set it up, because the way it is set up now the user has to input a number ranging from 1.1-1.4(activity level range) and from there it calculates it.
From my comment, you presumably want just this:
<label>Activity level
<select name="protein_activity">
<option value="1.1">Lightly Active</option>
<option value="1.2">Moderately Active</option>
<option value="1.3">Very Active</option>
<option value="1.4">Extra Active</option>
</select>
</label>
Another possibility you may be interested is a range slider, but you would have to modify your PHP side to accommodate that the form would send from 1-4 instead of 1.1-1.4 but it might add a user experience. This is just a quick and dirty example of a range. Note range is only available in HTML5:
<style>
input[type=range] {
width: 300px;
}
ul.ranger {
width: 300px;
display: table;
list-style: none;
margin: 0;
padding: 0;
}
ul.ranger li {
display: table-cell;
text-align: center;
width: 75px;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
padding: 0;
}
ul.ranger li:last-child {
text-align: right;
}
ul.ranger li:first-child {
text-align: left;
}
</style>
<form>
<input type="range" min="1" max="4" value="2" id="slider" name="protein_activity">
<ul class="ranger">
<li>Light</li>
<li>Moderate</li>
<li>Heavy</li>
<li>Extreme</li>
</ul>
</form>

Categories