Jump to content

GET & POST in javascript


robert_gsfame

Recommended Posts

i have this

 

 

<form name="form1" method="post">

<input type="text" id="textbox">

<input type="button" onclick="function()">

</form>

 

Let say function() will validate the textbox, if textbox is empty then alert box will appear and once filled it will go to the page whatever i put inside this function window.location()

 

I usually get the textbox value as i parse it through the url, and later i use $_GET to get the value....my question : is it possible to get the value using $_POST instead of getting it through url using $_GET

 

THANKS

Link to comment
https://forums.phpfreaks.com/topic/184780-get-post-in-javascript/
Share on other sites

you could change your form to something like this:

 

<form id="form1" action="somepage.php" method="post">
<input type="text" id="textbox1">
<input type="button" onclick="someFunction()">
</form>

 

then your javascript would look like this:

 

function someFunction(){
//get the form
var form1 = document.getElementById('form1');

//check the form
if(document.getElementById('textbox1').value != ''){
  
  //submit the form
  form1.submit();
} else {
  
  //show alert on error
  alert('Please fill in text box');
}

}

 

Basically all this does is submit the form after checking that your criteria is met. It will submit it as a POST instead of a GET. Please ask if you have any questions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.