php


How do I trigger/capture an event from my PHP generate HTML Code?


My class...
<?php
class SelectionBoxbuilder
{
public function RenderToHTML()
{
$SelectBox = '<select>
<option>"One"</option>
<option>"Two"</option>
</select>';
return $SelectBox;
}
}
My PHP test file that generates the selection box...
<?php
foreach (glob("classes/*.php") as $filename)
{
include $filename;
}
$sb = new SelectionBoxbuilder();
echo $sb->RenderToHTML();
This Works, but I need to know how to capture the event from the Selection box I have generated, not sure if I am in fact approaching this from the wrong angle perhaps I need to do this in an entirely different manner?
I basically want the event to trigger some other PHP code I have not written yet and pass the value of the selection box as a parameter.
Hope this I clear enough if not please let me know any additional information I could add.
So...
When the user changes the item selected on the selection box I would like this to trigger an event, which I will then 'point' to some other PHP code.
I am assuming that you are talking about change events on the select box (i.e. when the user selects a different value).
The main thing you need to understand is that the event is fired on the client side in the browser, while your PHP code is running on the server. It has no way of knowing what's happening on the client.
If you need to do something in PHP when these events are fired, you can add JavaScript code to handle the client-side event and to fire an AJAX request to your server-side PHP script.
In basicly PHP is a server side language.
So after the server side send output to browser you can't use PHP anymore.
To get the selection from user you need to make a new request to server.
You can do it by two methods:
Send the form data to server by regular http request (GET/POST).
Send data with ajax request for UX reasons.
First you need to change your "SelectionBoxbuilder" class to form element.
Like this:
<?php
class SelectionBoxbuilder{
public function RenderToHTML(){
$SelectBox = '
<form method="post">
<select name="selectbox">
<option value="1">"One"</option>
<option value="2">"Two"</option>
</select>
<br/>
<input type="submit" value="Send" />
</form>
';
return $SelectBox;
}
}
Now you have a form in you HTML output.
To get the response we need to add handler for the post request to the test file:
<?php
foreach (glob("classes/*.php") as $filename){
include $filename;
}
$sb = new SelectionBoxbuilder();
// If form submited
if( isset( $_POST['selectbox'] ) ){
echo 'Your selection is: ' . $_POST['selectbox'];
}
echo $sb->RenderToHTML();
You can add new method to your class to take care of the response:
<?php
class SelectionBoxbuilder{
public function RenderToHTML(){
$SelectBox = '
<form method="post">
<select name="selectbox">
<option value="1">"One"</option>
<option value="2">"Two"</option>
</select>
<br/>
<input type="submit" value="Send" />
</form>
';
return $SelectBox;
}
//Check response and return the value if form already submit
public function checkForResponse(){
if( isset( $_POST['selectbox'] ) ){
return 'Your selection is: ' . $_POST['selectbox'];
}
// return false if form not send
return false;
}
}
And now in test file you can do somthing like this:
<?php
foreach (glob("classes/*.php") as $filename){
include $filename;
}
$sb = new SelectionBoxbuilder();
// If form submited
if( $userAnswer = $sb -> checkForResponse() ){
echo 'Your selection is: ' . $userAnswer;
} else {
echo $sb->RenderToHTML();
}
I hope I was helpful to you.

Related Links

how to submit a form to another page in wordpress plugin
Storing Value from a Query PHP MYSQL
PHP to post SQL command will only create and delete table
how to return more then 1 variable data from php user define function
PHP: Code won't work without oci_connect()
modify permissions for files created by php script
php script works in browser but trows fatal error at command line
In Magento what is the correct data flow from POST to controller to model to block to template?
Login in Android to an Api System
echo correct message without slash
only want to show some of the text
PHP View Counter
Getting the pdf invoice from WHMCS?
uploading videos into a folder and its link to database using php
Choosing random data from an array (PHP)?
PHPExcel date getting substracted on some rows

Categories

HOME
webview
artificial-intelligence
cucumber
dotnetnuke
seedstack
android-fragments
postgresql-9.4
mousemove
openlayers-3
magento-2.0
websocket
twitter-fabric
fedora
wagtail
jpa-criteria
stack-overflow
driver
webstore
mahout-recommender
texas-instruments
vmware-workstation
mixpanel
wsf
akka-persistence
dashdb
hdl
el
sharedpreferences
onchange
php-5.3
ninject
sparkle
argv
advanced-filter
serversocket
image-optimization
char-pointer
db2-luw
wdk
fastlane
game-center
angular-datatables
word-cloud
virtual-memory
dredd
sonarqube5.2
icecast
nonlinear-functions
extjs4.1
deedle
connect-direct
degrees
yargs
google-identity-toolkit
e
always-on-top
powerpoint-2013
fdt
trust
google-maps-api-2
embeddedwebserver
relational-model
textscan
kefir.js
shoes
r.java-file
jolie
string.format
ctp
discovery
grails-2.3
iirf
donut-chart
stretch
chronometer
kaazing
kobold2d
nsfont
wcf-callbacks
fork-join
hungarian-notation
raw-data
main-method
comment-conventions
regioninfo

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App