0

I have searched the questions and none directly addresses the issue I'm having. Please forgive me if it has been addressed; I could not find it. I am making a form using JavaScript and trying to submit it, but when I do form.submit nothing happens. The page reloads, but it never gets to the PHP page to which I am directing it.

the JavaScript:

 function voteForName(name){ var form2 = document.createElement("form"); form2.setAttribute("method", "get"); form2.setAttribute("action", "../php/namesSuggest.php"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("type", "text"); hiddenField.setAttribute("name", "babyName"); hiddenField.setAttribute("value", name); form2.appendChild(hiddenField); document.body.appendChild(form2); alert(form2.outerHTML); form2.submit(); } 

the html

 '<tr> <td width="33%"></td> <td width="33%" align="center"><a href><p onclick="voteForName(\''. $obj["name"] .'\')" title="Click to vote for this name!" id="123'. $obj["name"] .'" >' . $obj["name"] . '</p></a></td> <td width="33%"></td> </tr>'; 

the html is being formed using php. the folders are set up

site home fileWhereJSIsCalled.php php namesSuggestions.php js myJSfile.js 

Any suggestions would be appreciated.

3
  • do you have any errors in the console? Commented Nov 14, 2013 at 4:29
  • i am not getting any errors in the console. I just figured it out... the 'href' in the <a> tag was not letting it do what it needed to do Commented Nov 14, 2013 at 4:37
  • such as silly mistake Commented Nov 14, 2013 at 4:37

2 Answers 2

1

try this

form2.setAttribute("action", "php/namesSuggest.php"); 
Sign up to request clarification or add additional context in comments.

Comments

0

Adding href to the <a> tag would refresh the page and not let the javascript do it's thing.

Very silly mistake, but I am going to add the answer here just in case anyone else has this problem

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.