Jump to content

[SOLVED] passing value from text input to button


saeed_violinist

Recommended Posts

Hi, I want to add a simple text input and a button in a page so users can set number of goods that they want to add to shopping cart, I came up with this but I cant gu further! I know this must be done with help of jscript so Im looking for your help, friends!

 

<html>
<body>

<form>
number of goods to put in cart<input name=number type=text value=1 >
<input type=\"button\" value=\"ADD TO CART\" onclick=\"window.location.href='cart.php?id=1?number=2'>
</form> 

</body>
</html>

 

Now how can I determine and use the number inputet by user in text input? to use it in my query string?

 

thanks.

Link to comment
Share on other sites

Are you combining a php shoping cart script or are you just using javascript? If you just want to change the text on your HTML page without a refresh you should look for tutorials of javascript DOM. the function getElementById().innerHTML and getElementById().value etc are the ones you will need to use i think

Link to comment
Share on other sites

thanks for your reply,

 

actually I have my own cart class that add an item to cart with a query like this:

 

http://www.shop.com/cart.php?action=add&id=1&number=1

 

its clear that the number=1 is the number of same item to be added in cart, the default number is 1, but I want to have a text input beside of links that genereate query string and read the nymber value thay user has entered and add it to the end of query string.

 

I mean how can I determine a value enteret in a text input, with out submiting form. hope you can helo me guys.

Link to comment
Share on other sites

With you php script it is a bit more complicated then just using javascript. But first things first. From a input field to javascript seems to be the first thing to do. this is how you can get a input value to javascript variable

 

<script>
function getProductNr(){
var productNr;
//this is where you retrieve the value from the input field with the id product_number
productNr=document.getElementById('product_number').value;

//alert the value to see if it worked
alert(productNr);
}
</script>
<!--be sure that the input field has an id-->
<input id="product_number" type="text" />
<!--button that calls the function getProductNr()-->
<button onclick="getProductNr()">get the number</button>

 

you will need to check if the product number is a number you can find plenty of scripts for that. Guess this should be your first step.

Link to comment
Share on other sites

ok ok very thanks I got the point,

 

Now, the problem is how can I use getProductNr() function to add the number to the query. my button for submiting query is as follow :

 

<input type=\"button\" value=\"ADD TO CART\" onclick=\"window.location.href='cart.html?action=add&id=" . $id . "&no=getProductNr()'>

 

the button will return :

cart.html?action=add&id=1&no=getProductNr()

 

!

Link to comment
Share on other sites

just add an id to the text field

<input name=number id=number type=text value=1 >
<input type=\"button\" value=\"ADD TO CART\" onclick=\"window.location.href='cart.php?id=1&number='+document.getElementById('number').value\";>

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.