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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.