0

After I add a button and a text field, how can I program that button to simply take what's in the text box and put it into a variable? I have no idea how the button click event works.

<form id="form1" name="form1" method="post" action=""> <label> <input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="xxxxxxxxx " /> </label> <label> <input type="text" name="txtboxsearchbydistro" id="txtboxsearchbydistro" /> </label> </form> 

Would I put a PHP statement in the space where the xxxxxxxx is at?

Any help would be great!

1
  • That would be JavaScript, not PHP. PHP would handle form data if you allow the submit button to reload the page. The onclick event would be handled by JavaScript functions. Commented Nov 11, 2011 at 19:31

2 Answers 2

3

You can't execute PHP code in onclick() statements because PHP gets executed on the server, before the page is sent to the browser, and the onclick() function is exectued at the browser.

Solution would be (assuming this page is form.php) set the action of the form for "form.php" and on that page have

if(isset($_POST)){ $variable = $_POST['txtboxsearchbydistro']; // Here you can run validation on $variable, sanitize it and pass it to a DB query } 
Sign up to request clarification or add additional context in comments.

2 Comments

Obviously, this is an example only... Passing $variable to your DB could cause great damage by SQL Injection. It's a great start, though.
Absolutely, agreed. For the sake of simplicity I removed it, however, I just edited the comment line to reflect the proper procedure
0

No, php is server-side, and onClick is client-side event.

I'm not entirely sure what are you trying to accomplish. If you wish to submit your txtboxsearchbydistro value to some PHP script, you would put something like this:

<form id="form1" name="form1" method="post" action="somePhpScript.php">

Then you would use something like Bobby proposed.

If you wish to do something before you actually send the form, or you want to do something on client side (i.e. in visitor's browser), you'd need to do something like

<input type="submit" name="Searchbydistro" id="Searchbydistro" value="Submit" onclick="myScript();" />

You could then need to define your script, and assign your value there.

Hope it helps.

1 Comment

Thank you! Im beginning to catch up on this client side and server side business!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.