Jump to content

[SOLVED] Yanking values from a text field without a form?


vozzek

Recommended Posts

Hi everyone,

 

Does a standard html text field have to be located within a form in order for me to pull values from it within php?  For example:

 

I have a text field for quantity.  The user can change it.  Later on in my page there's a button that launches the user to a php script on another page, and I need to know what the value of that text field was when the button was pressed.  There's not a form on my page, so I'm assuming I can't use $_GET or $_POST.

 

Is this possible?

 

Thanks in advance.

 

Link to comment
Share on other sites

The only way to get information from a browser to a server is to make a HTTP request to the server and include that information as part of the request.

 

The ways of making a HTTP request are a form that is submitted (POST or GET), a URL that is requested (GET), or using AJAX techniques (POST or GET.)

Link to comment
Share on other sites

Okay, at the risk of getting laughed out, I'll try to explain why I don't want to use a form.  ;)

 

I have four buttons on my view_cart page:  UPDATE CART, EMPTY CART, CONTINUE SHOPPING, and CHECKOUT.

 

Empty cart is a button that launches to a new php url called delete_from_cart.php.  I pass the cart id number over using $cid.  Looks like this:

 

<input type="image" src="Images/button_empty_cart.jpg" name="Empty" value="Empty" onmouseover="this.src='Images/button_empty_cart_rollover.jpg'" onmouseout="this.src='Images/button_empty_cart.jpg'" onclick="window.location.href='delete_from_cart.php?cid=0'" />

 

Continue shopping is a button that launches the user back to the last category they were shopping in.  The variable $lastcat is equivalent to the last url they were at.  The button looks like this:

 

<input type="image" src="Images/button_continue_shopping.jpg" name="Continue" value="Continue" onmouseover="this.src='Images/button_continue_shopping_rollover.jpg'" onmouseout="this.src='Images/button_continue_shopping.jpg'" onclick="window.location.href='<?php echo $lastcat; ?>'"

 

The Checkout button is the most simple, because it's does nothing more than call another url, no parameter passing or variables needed:

 

<input type="image" src="Images/button_checkout.jpg" name="Checkout" value="Checkout" onMouseOver="this.src='Images/button_checkout_rollover.jpg'" onMouseOut="this.src='Images/button_checkout.jpg'" onclick="window.location.href='checkout.php'" />

 

But my real problem lies in the Update Cart button.  There can be any number of items in the cart, so I need to pass an array to my update_cart.php script.  Originally I was using a form to post this to the next page, but for some reason it made every button on my entire page turn into a POST form button that launched me into update_cart.php.  It didn't matter where I placed the <form> </form> code, it assumed all buttons were supposed to post.  Dunno why.

 

I deleted that code and went with the onclick="window.location" stuff.  I made a small $SESSION array to contain the quantity information based upon cart id.  The only two variables I need to do the MySQL update are cart id and quantity.

 

Am I using redirection to these php files in order to make writes to my database?  Yes.  Am I doing this on separate pages to avoid using Ajax?  Yes.  ;D  Did I make an honest attempt to learn Ajax and incorporate it into my shopping cart?  Definitely yes.  But unfortunately it got rather complicated, took too much time, and I needed to get moving.  My shopping cart isn't all THAT elaborate, and I just wanted to get going on it.

 

The update cart button launches to the update_cart.php file and passes the total item count.  (I use this variable to loop through my global array):

 

<input type="image" src="Images/button_update_cart.jpg" name="Update" value="Update" onMouseOver="this.src='Images/button_update_cart_rollover.jpg'" onMouseOut="this.src='Images/button_update_cart.jpg'" onClick="window.location.href='update_cart.php?itemct=<?php echo $itemct; ?>'" />

 

So in the end, I just need a way to grab the text field I'm using for QUANTITY so I can post it to my session variable for that cart id (each cart id has its own quantity field).  I thought maybe I could use a javascript OnChange event to launch into a small script that would set the variable, but without the field being immersed inside a form, I don't know how.

 

I'm kinda new at all this stuff, so try not to smack me around too hard.  ::)

 

 

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.