Confused why form is not submitting - php

I don't think there's anything I left out, but the form doesn't seem to be transmitting any data upon hitting submit. My code is as follows, I realize it's kind of long, but I wanted to include the entire form. All of the attached functions just check if the input was valid:
<form name="formname" id="formname" action="database.php" method = post onsubmit="return checker()">
Username: <input type='text' id='un' onchange="checkname(id)" onkeyup="checkempty(id)" ><div id="un1"></div><br>
Reporter Title:<br>
<select id="s2" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="Assistant Director">Assistant Director</option>
<option value="Director">Director</option>
<option value="DB Admin">DB Admin</option>
<option value="Systems Admin">Systems Admin</option>
</select><div id="s21"></div>
<br>
Password: <input type='password' id='pw' onchange="checkpass(id)" onkeyup="checkempty(id)"><div id="pw1"></div><br>
Email: <input type='text' id='eml'onchange="checkemail(id)" onkeyup="checkempty(id)"><div id="eml1"></div><br>
Description:<br> <textarea rows="6" cols="20" id="desc" onchange="checkdesc(id)" onkeyup="checkempty(id)" ></textarea><div id="desc1"></div><br>
Type of Incident:<br>
<select id="s1" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="afs">afs</option>
<option value="password">password</option>
<option value="hardware">hardware</option>
<option value="other">other</option>
</select><div id="s11"></div>
<br>
<?php
include("connect.php");
$ret = mysql_query("SELECT * FROM countries");
echo "Choose your location:<br>";
echo "<select id='countries' onblur='checktype(id)'>";
echo "<option value='choose'>Choose one</option>";
while($array = mysql_fetch_array($ret)){
echo "<option value='{$array['abbrevs']}'>{$array['full']}</option>";
}
echo "</select><br>";
?>
<div id="countries1"></div><br>
<p>
Would you like an email copy?
<select id="s3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<input type="submit" id = "sub" value= "Submit">
</p>
</form>
and the php I tried to receive it with
<?php
include("connect.php");
$username = $_GET['un'];
$password = $_GET['s2'];
$reporter = $_GET['pw'];
$email = $_GET['eml'];
$description = $_GET['desc'];
$type = $_GET['s1'];
$country = $_GET['countries'];
$emailopt = $_GET['s3'];
?>
the checker function:
function checker(){
if(isgood && isgood1 && isgood2 && isgood3 && isgood4 && isgood5 && isgood6)
{
return true;
}
else{
document.getElementById('subb').style.visibility="visible";
document.getElementById('subb').innerHTML = "You must fully complete the form.";
return false;
}
where the "isgoods" where just quick flags I made for validations, which was all working properly.

Your form's method is POST so you should use $_POST instead of $_GET in your PHP script.
Also you should fix up your HTML so it's valid, you need to quote the method attribute properly:
<form name="formname" id="formname" action="database.php" method="post" onsubmit="return checker()">

Well, I feel stupid. The problem was that I didn't give my HTML elements names, but rather just IDs. I was so used to using ajax and submitting by just getting the value with javascript via the ID that I forgot about names O.o

You are submitting your form with method="POST", so in your PHP you need to read the POST values:
$username = $_POST['un'];
...
Had you submitted your form with method="GET", your PHP code would work. Read about the difference here.

Related

How to send query with select tag through url

I want send the data I received from the select tag through the url as queries so I can use it and query the database.
I have tried but the values are not showing. I am getting something like: type=&action=
This is my code
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
<a class="site-btn fs-submit" href="search.php?<?php echo $query; ?>"> Advanced search</a>
I expected :
type=2 bed room&action=rent
Try the following code. Here, When you submit the form the whole form data will be passed to search.php and the form method is POST so you can access those data by simply calling $_POST['attribute value of name'].
HTML form:
<form class="filter-form" method="post" action="search.php" enctype="multipart/form-data">
<input type="text "class="d-block d-md-inline" placeholder="Enter State, City or Area" name="city">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<input type="submit" value="Advanced search">
</form>
create a PHP file (search.php):
<?php
//Do what you want here
if (isset($_POST['city'])) {
$city = $_POST['city'];
echo $city;
}
if (isset($_POST['types'])) {
$type = $_POST['types'];
echo $type;
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
echo $action;
}
?>
Replace the link with a button. Attach the query params in the link to the action of the form. Your code should look like below.
<form action="search.php?<?php echo $query; ?>">
<select name="types">
<option value="2 bed room">2 bed room</option>
</select>
<select name="action">
<option value="rent">rent</option>
</select>
<?php
$type = $_POST['types'];
$action = $_POST['action'];
$query = "type={$type}&action={$action}";
?>
<button class="site-btn fs-submit" type="submit">Submit</button>
</form>

How can I get the values from the PHP generated forms?

I dont know where or how to use $_POST to get the values of the forms.
This is the whole code, I uploaded it on GitHub if you want to see the whole thing
This is the file I am trying to get the values out of.
new.php
<?php
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>
This is the page of the first dropdown, which gives me the value of the switch
adminpanel.html
<div class="dropdowns">
<form action="" method="post">
<select name="actiune" onchange="this.form.submit()">
<option>Selecteaza actiunea</option>
<option value="addElev">Adauga elev</option>
<option value="addNota">Adauga nota</option>
<option value="addAbsenta">Adauga absenta</option>
<option value="addTeza">Adauga teza</option>
<option value="deleteElev">Sterge elev</option>
<option value="deleteNota">Sterge nota</option>
<option value="deleteAbsenta">Sterge absenta</option>
<option value="deleteTeza">Sterge teza</option>
<option value="modifyElev">Modifica elev</option>
<option value="modifyTeza">Modifica teza</option>
<option value="modifyPurtare">Modifica nota purtare</option>
</select>
<?php include('new.php') ?>
</div>
When you submit a form that has a method of post, e.g.
<form method="post">
(note: action="" is redundant and can be left out)
the target page (in this case, itself) will be provided with the $_POST array.
You can dump the entire array to look at the values by using:
if (!empty($_POST) and isset($_POST['actiune']))
{
var_dump($_POST);
}
This array should contain all the values from within the <form> tag, provided a name has been set (which will be used as the 'key' in the array).
Not sure what is the switch for, added the fetch of postvars at the top of code. I am sure you can do some php to make sure vars are set. Also actiune is not a postvar that is its not the name of anyof the input fields so dont do $_POST['actiune']. it wont work. Else you have to add a hidden field, I did it here for you.
<?php
if (!empty($_POST) {
$numeElev = $_POST['numeElev'];
$orasElev = $_POST['orasElev'];
$sexElev = $_POST['sexElev'];
}
if (!empty($_POST) and isset($_POST['actiune']))
{
$actiune = isset($_POST['actiune']) ? $_POST['actiune'] : NULL;
switch($actiune){
//CASE 1
case 'addElev': echo '<div class=dbFormText>Adauga elev</div><div
class="databaseform"><form action="" method="post">
<input type="text" name="numeElev" placeholder="Nume elev"/>
<input type="text" name="orasElev" placeholder="Localitate"/>
<input type="hidden" name="actiune" placeholder="1"/>
<select name="sexElev">
<option value="Baiat">Baiat</option>
<option value="Fata">Fata</option></select>
<input type="text" name="tlfElev" placeholder="Nr. de telefon"/>
<input type="text" name="birthdayElev" placeholder="Nastere(DD-MM-YYYY)"/>
<input type="submit" value="Submit">
</form></div>';
break;
}
}
?>

Select element - Post method - return of value / Php

I'm having a hard time to fix and how can make my codes work well.
My textbox echo correctly while my dropdown box is not.
Can anyone help me and also clean my codes?
I wanna know how did you do it and can u please explain it to me.
Thank you so much.
index.php
<?php include 'test.php' ?>
<form method="post" action="index.php">
Textbox: <input type="text" name="txt1" value="<?php echo $txt1;?>">
Dropdown: <select name="drpdown1" value="<?php echo $drpdown1;?>">
<option></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
</select>
<input type="submit" name="btn1">
</form>
test.php
<?php
$txt1 = "";
$drpdown1 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$txt1 = $_POST["txt1"];
$drpdown1 = $_POST["drpdown1"];
}
?>
You're not echoing the value of $drpdown1 correctly:
// this is wrong for a select:
<select name="drpdown1" value="<?php echo $drpdown1;?>">
// etc.
If you want to select automatically the previously selected value, you need to add the selected attribute:
<select name="drpdown1">
<option value="1" <?php if ($drpdown1 === '1') { echo "selected='selected'"; } ?>>Mark</option>
<option value="2" <?php if ($drpdown1 === '2') { echo "selected='selected'"; } ?>>Extreme</option>
// etc.
you have to know more about the dropdown box because you can not put the value inside the
<select value="<?php echo $drpdown1;?>">
you have to compare the value inside the option directly. example
<select name="drpdown1">
<?php
if($drpdown1 == ""){
?>
<option selected></option>
<option value="1">Mark</option>
<option value="2">Extreme</option>
<?php
}else if($drpdown1 == "1"){
?>
<option></option>
<option value="1" selected>Mark</option>
<option value="2">Extreme</option>
<?php
}
?>
</select>

html and php form field and submission

I have a short html form and some php code on the same page so that when the form is submitted, the entries appear on the same page. This code works as far as posting the entered information from the form to the page, but I have 2 problems:
The text box for some reason was only letting me enter 1 character, now it won't let me enter any characters.
Every time I refresh the page to try the form again, the information keeps appending. I only want/need for it to show up once after submission.
<form method="post" action="">
<label>Select Out of Office Message
<select name = "selectoutofofficemessage">
<option value = "N/A">N/A</option>
<option value = "Vacation">Vacation</option>
<option value = "Conference">Conference</option>
<option value = "Meeting">Meeting</option>
<option value = "Other">Other</option>
</select>
<label>Custom Out of Office Message
<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
</label>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = file_get_contents("posts.txt");
$posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
file_put_contents("posts.txt", $posts);
echo $posts;
?>
Put a validation to the Post values
if(isset(selectoutofofficemessage))
{
execute the html code;
}
else
{
php code....
} then the value will not be repeated again and again and dont append it just display it....
1.First label is incorrect, it doesn't close. Fix it:
<label>Select Out of Office Message
<select>
...
</select>
</label>
2.Change your PHP code:
<?php
$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";
if(!empty($_POST))
{
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage\n" . $posts;
file_put_contents("posts.txt", $posts);
}
echo $posts;
Consider each page refresh after a submition causes the last submition.
<form method="post" action="">
<label>Select Out of Office Message</label>
<select name = "selectoutofofficemessage">
<option selected="selected" value="">Select a Message</option>
<option value ="N/A">N/A</option>
<option value = "Vacation">Vacation</option>
<option value = "Conference">Conference</option>
<option value = "Meeting">Meeting</option>
<option value = "Other">Other</option>
</select>
<label>Custom Out of Office Message</label>
<input type="text" name="customoutofofficemessage" size="30" maxlength="255"/>
<p>
<input type="submit" name="submit" value="Submit" />
</p>
</form>
<?php
$posts = file_exists("posts.txt") ? file_get_contents("posts.txt") : "";
if(!empty($_POST))
{
$selectoutofofficemessage = $_POST["selectoutofofficemessage"];
$customoutofofficemessage = $_POST["customoutofofficemessage"];
$posts = "$selectoutofofficemessage - $customoutofofficemessage AT <small><em>". date('h:m A - M-d-Y') ."</em></small> <br>" . $posts;
file_put_contents("posts.txt", $posts);
}
echo $posts;
?>
<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
window.location = window.location.href;
</script>
<?php } ?>

display the output in the same page using php

im new to php.i created a page in php containing chk boxes ,input fields and retrive the data from the database(mysql) and also use the javascript.
i h've a problem in this ,that is i want to display the output in the same page.
i created the code like this,i want the output in same page,but it shows error.
<?php
if(isset($_POST['submit']))
{
$con=mysql_connect("localhost","demosdef_review","review123");
if(!$con)
{
die('could nt connect 2 the server');
}
mysql_select_db("demosdef_review", $con);
$state = $_POST["state"];
$country = $_POST['country'];
$contry = "USA";
if($country==1)
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='1' AND id='$state'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
else
{
$result = mysql_query("SELECT address FROM storelocator WHERE countryid='$country'");
$row = mysql_fetch_array($result);
echo $row['address'];
}
}
?>
enter code here
<div class=buy>
Dealer Enquiry
</div><br/>
<div class=buyfont>Authorized Retailers</div>
<div><img src="images/archivers.png"><br/>
<a href="http://www.archiversannex.com/Books-And-SoftwareSoftware-And-Accessoriesdefault.aspx?PageID=20&CategoryID=48"/>
<img src="images/logo_archivers_annex.png" ></a><br/><br/>
<br/><br/>
</div><br/>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>
<p>
<label for="country" style="padding-right: 2px;">Country</label><select name="country" value="countryid"
id="countryid"
onchange="sub()" style="width:120px;">
<option value="1">USA</option>
<option value="2">CANADA</option>
<option value="3">UK</option>
<option value="4">AUSTRALIA</option>
<option value="5">ITALY</option>
<option value="6">GUATEMALA</option>
<option value="7">NEW ZEALAND</option></select></p>
<p>
<label for="state" style="padding-right: 14px;">State</label>
<select id="state" name="state" value="state" style="width:120px">
<option value="7">Alabama</option>
<option value="8">Alaska</option>
<option value="9">Arizona</option>
<option value="10">Arkansas</option>
<option value="11">California</option>
<option value="12">Colorado</option>
<option value="13">Connecticut</option>
<option value="43">Florida</option>
<option value="14">Georgia</option>
<option value="15">Idaho</option>
<option value="16">Illinois</option>
</select></p> <br/>
<input class="dealer-submit" type="submit" value="Submit" onClick="buy_func()"/>
</form>
<script type="text/javascript">
function sub()
{
var x=document.getElementById("countryid").selectedIndex;
var y=document.getElementById("countryid").options;
var z = document.forms[0].state;
if(y[x].index==0){
z.disabled = false;}
else if(y[x].index>0) {
z.disabled = true;}}
</script>
Your HTML markup has an error
<a href="http://www.hobbylobby.com/storelocator"/>
It should be
<a href="http://www.hobbylobby.com/storelocator">
Also try
<form method="POST" action="">
Keeping the action blank will post it to the same page itself
And replace your submit button with this
<input class="dealer-submit" type="submit" name="submit" value="Submit" onClick="buy_func()"/>
Check the value of the submit button, if its set isset($_GET('submit')) then display the results
does that solve your problem?
submit is the type of the element, 'Submit' is the value you are looking for:
<?php
if(isset($_POST['Submit']){
}
?>
Remove the backslash at the end of the line:
<a href="http://www.hobbylobby.com/storelocator"/>
^

Categories