Input form - can inputs be accessed as variables?

2019-06-01 11:53发布

Edit to give overview of my goal:

When user hits the submit button, I want the amount they entered and hidden fields to go to Paypal add to cart. I want that same click to send info they typed into textarea to be emailed to our email. They are donating an amount of their choice for use to send a sympathy/birthday card in their name. There are other paypal options on the page that get added to cart too.

I'd like to skip js if possible so they don't have to have it activated (it's not needed anywhere else on the site.)

I can send to php but found that opens a new browser tab even when set to target="_self" so it ruins the flow.

If I could do two actions from an input form, and have the php not display a separate page... that would do the trick. I've been searching but haven't found solutions for either. Most "two actions" are for two different buttons to choose from.

Thanks for reading and helping!

================= Original question:

In the same php/html file that contains the input form -- can the variables that were gotten from the user in the input form -- be accessed? How?

So

<input type="text" name="amount">

Can I then access the amount? Either access it inside the same input form, before the submit field. Or access it after the submit, outside after, but in the same file. Since the input form's action is sending it to paypal, I don't want to send it to a file of my own and process it there.

I can't figure out the syntax for grabbing the variable amount.

I've been searching for days, but can't seem to find a search that gives me anything except what to do if it's passed to another PHP file. Or tells me how to set this to variables. I'm a programmer but new enough to html, that I may be missing the obvious.

Thanks!

标签: forms input
2条回答
霸刀☆藐视天下
2楼-- · 2019-06-01 12:15

With the help here, and lots more searching, I came up with a solution that works for my application. It doesn't require javascript.

1) Solution must use two buttons because Forms can have only one action. In this case one is to Paypal, which I can't control or combine.

2) User fills out instruction in the textarea and submits it. Input form's action goes back to same file. Where PHP checks if textarea was submitted, and posts HTML message from within the PHP code asking user to finish by entering their donation and clicking a 2nd button. The donation amount and 2nd button go to paypal.

3) So inside the file: a) html to display Eventcard options b) input form to collect textarea c) that input form goes back to same file, on same page. So action="url of the current page" and target="_self"

These are because "_self" reloads on the same webpage. And action "to the same URL" gives the input form's $_POST back to the same page while refreshing it.

d) php uses an (if isset type code) to check two items. Both if isset ($_POST[Submitcard] == "value I gave submit element in input form") and also if textarea's value is <> "".

In other words there needs to be something entered in the textarea and a click of the submit button to process an event card by emailing the textarea to our cardperson. I also post an html message embedded in the php to the user to remind them to now added the cost of the card to paypal.

Initially I set a variable to the textarea's value. Then after it's processed, I set it to "". So that way I don't wind up needing to reset the $_POST. I check the variable on next time through the file.

e) Now the file has another input form, this one going to paypal and asking for the user to input the donation amount (with an input text field) and click the "add to cart" button. That sends the cost to the paypal cart. And if they want to add more to the cart from other selections on the page, they can hit paypal's "continue shopping". And paypal refreshes the page when it comes back to it.

===========================

It's coded and working. I learned: 1) I couldn't access the input form values inside the form (that I can figure out), but can get at the _POST values when the file is rest to itself in the input form. So my original question has an erronous assumption about accessing the variables.

2) EOL DOCS something, can completely wrap around html in php so you don't have \ in front of every quote. It needs those \'s removed to work right. The closing EOL; MUST be in the very left of the file. Seems irrational, but I tested and it's so.

3) Paypal at one point was deleting it's cart page when you "continued shopping" but wasn't refocusing on my site's page that it was being redirected to. Turned out that was an errant / at the end of the action="URL/". That slash tells that you are specifying a folder not a file, so it kept looking for a file and never found it. Works fine in other places, but Paypal didn't deal with it well.

4) If you run the file with the html embedded in php on the client side, it prints a mess of the file's contents fairly indiscrimantly. Sometimes it can be triggered by an error above that line, but other times the contents print out when they shouldn't -- but the same file works fine when you run the same file on the server side where PHP is sitting and is processed.

5) You can redirect a php action that calls a different php, not to print a blank page. It's a redirect command and I hadn't tried it out thoroughly yet.

6) I tried variations like entering donation first and wrong amounts (neg money) and so on, and nothing broke the system or sent more emails than actual textarea entries.

So for what it's worth that was my adventure that wound up with a working solution.

I still don't see a way to go to one button. Put if anyone does please post it!

查看更多
家丑人穷心不美
3楼-- · 2019-06-01 12:32

You'll need to use "id" and a little JavaScript. For example:

<input type="text" name="amount" id="amount">
<button onclick="alert('amount is: ' + document.getElementById('amount').value)">Show</button>

查看更多
登录 后发表回答