HTML Form Not POSTing? Has name field? - php

I feel like an absolute moron posting this question, but I'm completely serious, I officially have NO idea why this form isn't posting. I've gone from using AJAX to display information inputted to simple submit-redirect-grab Post data in PHP to $_SERVER["PHP_SELF"] trying to get the information to JUST POST, and it won't. Most of the people asking this question have had their answers revealed as "You need a name field" or "Your javascript is the issue" but I have a name field and I am no longer using javascript. I am utterly and completely confused.
For reference, I'm using PHPStorm and a Local server to test this out. I just logged on fresh today, saved the project, ran it for the first times today, made sure it wasn't just a sort of cached project, and it doesn't appear to have anything with old data.
Please don't mind if some of the form syntax is messy, I just want this darn post to work so I can move on to the meat of what I'm trying to make. Also there is no stylesheet currently, it's mostly empty apart from
.id {
height: 100px;
}
Also none of the form fields are required, you really only need to enter information into one. I've got it doing a var_dump just so I can see all of the form post data, and it's brilliantly not working. I did have it echoing out something in PHP along the lines of "Done!" to show php is being read, and I also had it attempting to echo $_POST["modelnumber"] just to see that. Nope.
index.php
<!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="index.css">
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Model Number:<br>
<input type="text" id="mod" name="modelnumber"><br><br>
Amazon Item?<br>
<input type="radio" id="amazonItemY" name="amazonItem" value="yes"> Yes<br>
<input type="radio" id="amazonItemN" name="amazonItem" value="no" checked> No<br><br>
Item name:<br>
<input type="text" id="intName" name="itemName"><br><br>
Item Description:<br>
<input type="text" id="internetDescription" name="description"><br><br>
Brand:<br>
<input type="text" id="brand" name="itemBrand"><br><br>
Manufacturer:<br>
<input type="text" id="manufacturer" name="manName"><br><br>
Website category:<br>
<input type="text" id="cmcategory" name="category"><br><br>
Clearance?<br>
<input type="radio" id="clearanceY" name="clearance" value="yes"> Yes<br>
<input type="radio" id="clearanceN" name="clearance" value="no" checked> No<br><br>
Competitor Price: (leave empty if not clearance)<br>
<input type="text" id="competitorPrice" name="compPrice"><br><br>
School price:<br>
<input type="text" id="catStdPrice" name="schoolPrice"><br><br>
Dealer price:<br>
<input type="text" id="dealerStdPrice" name="dealerPrice"><br><br>
Website price(normal):<br>
<input type="text" id="intCsiPrice" name="webPrice"><br><br>
Unit of Measure:<br>
<input type="text" id="unitOfMeasure" name="uom"><br><br>
UPC:<br>
<input type="text" id="upc" name="upcNum"><br><br>
Manufacturer Part Number:<br>
<input type="text" id="mfgr_num" name="manfacNum"><br><br>
Item Attribute:<br> <!--needs to be a dropdown-->
<select id="attribute" name="attributes">
<option value="(1593325)Baseball">Baseball</option>
<option value="(1593326)Basketball">Basketball</option>
<option value="(1593386)Boating + Paddling">Boating + Paddling</option>
<option value="(1593314)Camping Hiking + Climbing">Camping_ Hiking + Climbing</option>
<option value="(1610848)Cricket">Cricket</option>
<option value="(1593313)Cycling">Cycling</option>
<option value="(1593315)Equestrian">Equestrian</option>
<option value="(1593318)Fishing">Fishing</option>
<option value="(1593323)Football">Football</option>
<option value="(1594100)General Sports Equipment + Accessories">General Sports Equipment + Accessories</option>
<option value="(1593320)Golf">Golf</option>
<option value="(1593319)Hunting + Archery">Hunting + Archery</option>
<option value="(1593329)Ice + Roller Hockey">Ice + Roller Hockey</option>
<option value="(1593327)Other Sports">Other Sports</option>
<option value="(1593331)Personal Fitness">Personal Fitness</option>
<option value="(1599122)Pool + Billiards">Pool + Billiards</option>
<option value="(1593321)Rugby">Rugby</option>
<option value="(1593317)Skateboarding + Rollerskating">Skateboarding + Rollerskating</option>
<option value="(1593330)Ski + Snow">Ski + Snow</option>
<option value="(1593322)Soccer">Soccer</option>
<option value="(1593324)Softball">Softball</option>
<option value="(1593316)Tennis">Tennis</option>
<option value="(1593328)Water Sports">Water Sports</option>
</select><br/>
<input type="submit">
</form>
<?php
var_dump($_POST);
?>

RedRazor's answer is not a solution. The PHP interpreter resets the quotes inside of the php tags. This means that you can re-use the same quotes in the interpretation snippet.
This seems more like a PHP Storm issue, try to restart your PHP Storm server. I copied your code exactly to my machine and used PHP Storm built in server. With no modifications your code worked. I've attached some images of my setup.
Also you have to be running the PHP server to get $_POST requests, there is an icon to just open in the browser, which will work for many cases, but it will not work on requests like this due to their nature.
Here are some images of my setup:

Please make sure that there is no conflict between quotation marks ("")
Instead of
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Try
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">

Related

get value after onsubmit dropdown

I have a little bug I do not find the solution, I am close but he miss me something.
When I arrive on my product catalog page, I have this url
http://localhost/admin/index.php?A&Catalog\Products&Products
I click on my categories dropdown, My url become :
http://localhost/admin/index.php?A&Catalog\Products&Products&cPath=
As you see the value of cPath do not exist but if I display the value of echo $_POST['cPath']
I have my number
The problem, my link inside the form do not want to take the value, I need to click twice on my dropdown to have the $_POST['cPath'] value.
$post_cPath = '';
if(isset($_POST['cPath'])) {
if(!empty($_POST['cPath'])) {
$post_cPath = $_POST['cPath'];
}
}
echo $post_cPath; // I see this value after the first click but below it do not want to take the value, just after the second click.
echo HTML::form('goto', $products->link('Products&cPath=' . $post_cPath), 'post');
Thank you.
HTML code
arrive in the page
<form name="goto" action="http://localhost/Admin/index.php?A&Catalog\Products&Products&cPath=" method="post">
<select name="cPath" onchange="this.form.submit();" class="form-control form-select"><option value="" selected="selected">--- Haut ---</option><option value="14">Camera</option><option value="8">Components</option><option value="9"> Mice and Trackballs</option><option value="10"> Monitors</option><option value="2">Desktops</option><option value="1"> MacBook</option><option value="4"> PC</option><option value="3">Laptops & Notebooks</option><option value="5"> Mac</option><option value="6"> Windows</option><option value="13">Phones & PDAs</option><option value="12">Tablets</option></select> </form>
after the categories selection
The cPath has a value
<form name="goto" action="http://localhost/Admin/index.php?A&Catalog\Products&Products&cPath=13" method="post">
<select name="cPath" onchange="this.form.submit();" class="form-control form-select"><option value="" selected="selected">--- Haut ---</option><option value="14">Camera</option><option value="8">Components</option><option value="9"> Mice and Trackballs</option><option value="10"> Monitors</option><option value="2">Desktops</option><option value="1"> MacBook</option><option value="4"> PC</option><option value="3">Laptops & Notebooks</option><option value="5"> Mac</option><option value="6"> Windows</option><option value="13">Phones & PDAs</option><option value="12">Tablets</option></select> </form>
but my urk is always like that
http://localhost/admin/index.php?A&Catalog\Products&Products&cPath=
my cPath value is empty. I need a value after the first click and no after the second click

How to send a form only if a field is empty

Being a total newbie in PHP, i have issues with implementing the Google reCaptcha on my website.
I oriented myself towards an easier solution which would be: if this field is empty, SEND the form.
Here is my code:
<form method="POST" action="/#contact">
<!-- Name + mail -->
<input type="text" required placeholder="Name" name="name" />
<input type="email" required placeholder="Email" name="email" id="email" value=""/>
<select name="subject" id="category">
<option value="">Subject</option>
<option value="1">1</option>
<option value="1">2</option>
<option value="1">3</option>
<option value="1">4</option>
</select>
<input id="submit" name="submit" type="submit" value="Send"/>
<input type="reset" value="Clear" class="alt" />
<div>
<label>If you're human, leave this field epmty</label>
<input type="text" name="comment">
<?php
if(empty($_POST['comment'])) {
// the field is empty
}
?>
</div>
</form>
My problem is: the form is still being sent, even there is something in the field.
Thanks in advance for your help.
There are 2 solutions to this:
Call a php file that validates the inputs. When a user hits the send button, it goes to a php page. This page retrieves the values of the filled in form and validates it. If an invalid input is found, you can call the main page (ex. index.php) with an error parameter in the url (ex. index.php?error=botfound). And in the beginning of your index.php file, you can write a check if there is an error parameter and display the error message if you want.
Let javascript validate the values of the inputs before the values are sent to a php file. To do this, you change your code from <form method="POST" action="/#contact">
to:
<form method="POST" action="/#contact" onsubmit="DoSubmit();">
With a javascript function something like:
<script>
function DoSubmit(){
if(document.getElementById('idOfInput').value != ""){
alert("Wrong input"); //Or you can actually add an error message to your page.
return false;
}
return true;
}
</script>
I think that script will make sure if the value of the input with id 'ifOfInput' is not empty, it'll give an alert and will not call the php script.
(By the way, you can also make that input invisible so it doesn't annoy normal users but bots will still fill in the input.)
Hope this helped :)

Form not parsing correctly

I am having a ton of trouble with parsing A custom form that I created in wordpress, I am on a page called front-page.php where my form is located and the parser is on a page called formhandler.php, I have my parser running from a seperate page.
I am really confused because the form itself is recognizing the parser on the other page, just not actually parsing the data and taking you to the correct page. I get a long unknown URL that takes me to a 404 page within the site itself.
I need help figuring out why this forms code is not working or if the parser is just not in the right location to actually do the parsing work. I am fairly new to wordpress so anything helps right now. I have to get this done right now for my client, any help would be awesome. I just haven't had to use a custom form within WordPress before.
URL:
http://50.22.79.62/~pftech/formhandler.php?selection=http%3A%2F%2F50.22.79.62%2F~pftech%2Fwater-delivery-service%2F&zip=92115&home=on&did_submit=Get+Started%21
FORM PARSER:
<?php
if(isset($_GET['zip'])){
$sandiego = array('91911', '91914', '91915', '91932', '91942', '91945', '91950', '92014', '92025', '92027', '92029', '92037', '92064', '92065', '92067', '92071', '92075', '92101', '92102', '92103', '92104', '92105', '92106', '92107', '92108', '92109', '92110', '92111', '92113', '92114', '92115', '92116', '92117', '92118', '92119', '92120', '92121', '92122', '92123', '92124', '92126', '92127', '92128', '92129', '92130', '92131', '92132', '92134', '92135', '92139', '92140', '92145', '92147', '92154', '92173');
if (in_array($_GET['zip'], $sandiego)){
header("Location: ".urldecode($_GET['selection'])."?zip=".$_GET['zip']."&type=".$_GET['type']);
} else {
header("Location: http://www.pureflo.com/");
}
exit;
}
?>
FORM:
<form method='get' id='gform_1' action='front-page.php'>
<div class="serviceinput">
<label for="services">Services: </label>
<select id="selection" name="selection">
<option value=''>Select a Service</option>
<option value='http://50.22.79.62/~pftech/water-delivery-service/'>Water Delivery</option>
<option value='http://50.22.79.62/~pftech/coffee-delivery/'>Coffee Services</option>
<option value='http://50.22.79.62/~pftech/water-filtration-systems/'>Water Filtration</option>
</select>
</div>
<div class="zipcode">
<label for="zip">Zip Code: </label>
<input name="zip" type="text" maxlength="5" id="zip" />
</div>
<div class="frontradio">
<input name="type" type="radio" id="homeradio" value="home" />
<div class="homelabel"> <label for="homeradio">Home</label></div>
<input name="type" type="radio" id="officeradio" value="office" />
<label for="officeradio">Office</label>
</div>
<div class="homebutton">
<input type='submit' id="submithome" name="did_submit" value="Get Started!">
</div>
</form>
it seems that in the action, you have front-page.php which is the name of the form file. is this what you want to have? 404 means that the page is not found.
I believe you should try action='formhandler.php'. After is found, you can check your parser.

How to pass a variable from HTML form and DropList to the PHP file using "get"

I want to change this code...
<form action="search.php" method="get" name="form1" id="form1">
<select name="name">
<option value="category">CATEGORY</option>
<option value="product">PRODUCT</option>
</select>
<input name="value" type="text" id="catprod" size="20" />
At the end I want to receive a link of type...
http://www.mysite.com/search.php?product=car
or
http://www.mysite.com/search.php?category=auto
depends of what item from the drop list will choose a customer. "Car" and "Auto" are search requests from the customers, which they will type in.
Well, there are 2 methods of doing this.
The first one is change the input name of for text-field to the correct name (category or product) when de select is changed. So you make a
onChange="document.getElementById('catprod').name = this.value;"
Watch out that on loading you give the input the correct name already. Under normal conditions that would be "category" (because that is selected as first one).
Another option is to make 2 input fields with 1 hidden. And you show the field that the user has selected. (also with a onchange, but then using hide and show).
Anyway, this both would not be the best way to solve the problem. The best problem is just by solving it in your PHP script.
I would use the following:
<html>
<head>
<script>
function redirect() {
window.location.href = 'http://www.mysite.com/search.php?'+document.form1.key.value+'='+document.form1.val.value;
}
</script>
</head>
<form name="form1" onsubmit="redirect(); return false;">
<select name="key">
<option value="category">CATEGORY</option>
<option value="product">PRODUCT</option>
</select>
<input name="val" type="text" id="catprod" />
</form>
</html>
your javascript and html are mainly seperated this way.
<form action="search.php" method="get" name="form1" id="form1">
<select style="border:1px solid rgb(102,153,204);background-color:FFFFCC;height:20px;width:120px;" onfocus="this.style.background='white'" onblur="this.style.background='FFFFCC'" name="name" id="myselect">
<option value="category">CATEGORY</option>
<option value="product">PRODUCT</option>
</select>
<input name="value" type="text" id="catprod" style="border:1px solid rgb(102,153,204);background-color:FFFFCC;height:15px;width:200px;" onfocus="this.style.background='white'" onblur="this.style.background='FFFFCC'" size="20" />
<script>
var submitform = function() {
form1.submit();
}
document.getElementById('myselect').onchange = submitform;
</script>
I strongly suggest not using inline css as you're using and giving more meaningful names to your IDs (and classes). Also, avoid using inline JS.
What the code in the script tag does, is submit the form when you change the value on the select element. Since your form uses method "get", you'll have those types of URLs when you submit the form.

Is it possible to have a drop-down menu influence the returned value

I'm trying to set up an html form that allows the user to select a field from a drop-down menu, then specify the information for that field. For example, if they select "Name" from the menu, then enter a name, it posts a specific response.
Here's how the html looks:
<form action="hello.php" method="post">
<select>
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit">
</form>
What I'm trying to do is have the php code change responses based on what's submitted. Here's the php code:
$name = $_POST['name'];
$gender = $_POST['gender'];
$bday = $_POST['birthday'];
With the responses:
echo "Hello, $name!";
echo "Your gender is $gender!";
echo "Happy birthday on $birthday!";
depending on what option they select. (I haven't finished the if/elseif/else statements yet because I'm not sure if they're influenced by how the information is recieved.)
Is it possible to do this? I know that my code will only ever return $name in this example, because
<input name="name"...>
But I don't know if it's possible to have the text field's information identified by the drop-down menu. I imagine it's probably possible with Javascript, but I've never used Javascript and wouldn't know how to do this.
Thanks!
EDIT: I found an answer. I ended up setting
<input type="text" name="submit" />
in my html code, and
$submit = $_POST['submit'];
in my php code. Now if I do something such as
echo $submit;
it returns whatever I entered in the html page. Thanks again!
<form action="hello.php" method="post">
<select name="myselectbox">
<option disabled="yes" selected="yes">Select...</option>
<option value="name">Name</option>
<option value="gender">Gender</option>
<option value="birthday>Birthday</option>
<input name="name" type="text" />
<input type="submit" value="Submit" />
</form>
Then in hello.php do this at the top of the file.
var_dump($_POST);
that should help you understand the relationship between a html form and the PHP Postback Form Handler (in your case hello.php)

Categories