Reputation: 487
Having a bit of trouble here getting form.submit() to work normally. I'm looking into PHP for the first time and I'm sure how to locate my problem here. It looks like the Javascript is working normally. Whats strange is that if I create a normal everything works normally. Unfortunately what I need to do is call form.submit() as an event handler on another input button located outside the form.
<div id="top-header"></div>
<div id="form-container">
<div id="location-section">
<form id="customer-info" class="unselectable" unselectable='on' method="post" target="_blank">
Company name  
<input type="text" class="textInput" id="company-name" name="name">
<br>Street address
<input type="text" class="textInput" id="street-address" name="address">
<br>City
<input type="text" class="textInput" id="city">
<br>State/Province
<input type="text" class="textInput" id="state-province">
<br>Zip code
<input type="text" class="textInput" id="zip-code">
<br>Country
<input type="text" class="textInput" id="country">
</form>
</div>
I only put a name of two of the input elements for now, since I'm just trying to figure out what's wrong here.
Here's creating a variable for the form being submitted
var customerInfo = document.getElementById("customer-info");
I'm using some JQuery in this project so I have everything wrapped up in a $(document).ready();
Where Things get strange here is that my console.log() actually outputs "submit" normally, so this code is definitely being executed. This makes me thing my problem is something not so obvious (at least maybe not to me!)
submit.addEventListener("click", function(){
console.log("Submit");
customerInfo.submit();
});
Javascript
<?php
if(isset($_POST["name"]))
{
echo $_POST["name"];
}
else
{
echo "no name provided";
}
if(isset($_POST["address"]))
{
echo $_POST["address"];
}
else
{
echo "no address provided";
}
?>
PHP
The more I look at this the more confounding I it is. It doesn't look like I made any obvious mistakes in creating my variables, so I'm curious what might be causing these problems.
Upvotes: 0
Views: 59
Reputation: 3435
Your form does not have an action
attribute. Therefore it will not do anything when you submit. You need to add an action
attribute that hits that PHP endpoint.
Alternatively, add an onclick
listener to the form, and do whatever work you want it to do on submit from this end instead.
Either way, your form does not currently have anything set up to "do" when you submit it, which is why it is not working!
Edit: as some people are pointing out in the comments, the action
attribute is not required in HTML 5. That being said, the fact that this code is not working still leads me to believe you should investigate this as a possible cause
Upvotes: 1