Button press & release actions (php) - php

I want to create a button with press & release actions, so, for example, when i press it, it shows some text and when i release it it hides the text. Can anybody help?
What i have already tried:
<form action="forwardon.php" method="get">
<input type="submit" value="Forward">
</form>
This just creates a button which redirects to a page which is running the desired action: but you cant create press & release actions with this..
And i cant find any way how to do this.. Or is it just not possible with php?

Well, yes. It's not possible with php, you need to use javascript for that.
Every button has events: onmousedown and onmouseup. You can see the list of events here
Basically, you need to create functions, that will be fired in javascript while pressing/releasing. It would look like this
<html>
<-- your html here -->
</html>
<script>
document.getElementById('id-of-your-button')
.addEventListener('mousedown', function() {
//change the text you need here with pure javascript
});
</script>
The same goes with onmouseup event too.
To know more about javascript visit w3schools

Like James already said. In PHP it isn't possible to make something like button events, because PHP is a ServerSideLanguage.
As he also mentioned, you have to use for example JavaScript.
With the following script you are able to change the text with mouse events
<input type="button" value="Forward" onmousedown="getElementById('text').innerHTML = 'pressed';" onmouseup="getElementById('text').innerHTML = '';">
<p id="text"></p>

PHP won't be able to do this without refreshing the page/going to a new page.
JavaScript can give you what you are looking for.
see w3schools first example for showing text.
Hope this helps

Related

Increment counter onclick

I have set the number X to 5.
When clicking a button a script is run to update the database without refreshing - that works fine.
Now i want a number on the page to update to 6, also without the page refreshing.
I tried JS, but ended up having the number in an input field which looks bad. Is there any other way, maybe with jQuery.
This will let you get the element by its id and update its value.
document.getElementById('theId').innerHTML = newValue;
If you are not currently using jQuery then do not use it just for this. If you are using jQuery you can do the following.
$('#theId').html(newValue);
or
$('#theId').text(newValue);
Depending on whether the newValue may or may not have html in it.
Both the jquery and straight javascript examples are equivalent.
You don't have to use jQuery.
You can update everything on page by pure JavaScript, without jQuery.
Example from Javascript Tutorial at tizag.com:
<script type="text/javascript">
function changeText()
{
document.getElementById('boldStuff').innerHTML = 'Fred Flinstone';
}
</script>
<p>
Welcome to the site <b id='boldStuff'>dude</b>
</p>
<input type='button' onclick='changeText()' value='Change Text'/>
with jquery $("your containerselector").html(parseInt($("your containerselector").html()) + 1) Though you need to be sure your container just contains on int.

Show form from php file on one of my pages - how?

I've spend a week on this issue and it is probably real simple...
I am using osTicket (open source support ticket system) on my site. I want to use the ticketform as a contact form on one of my pages. I only want to show the form id="ticketForm" (see code below) because I don't want all the rest of the code (menu with Support Center Home, Open New Ticket, Check Ticket Status buttons etc.) from the open.php file - I tried to use an iframe, but too many problems and iframe is not a good way to do it.
I want to use this part (I don't show all the code because I think there is some sensitive information in the code - hope that is okay):
<form id="ticketForm" enctype="multipart/form-data" action="open.php" method="post">
<input type="hidden" value=......... etc. code for name, email, attachment fields, captcha etc.</form>
from:
www.mysite.com...../support/open.php
and show it on:
www.mysite.com...../contact.html
so that I get a contact form that blends in with the text I have on that page.
Any simple way to do this? Please be gentle (don't assume that I know things that you take for granted) - I'm no coder :o) Thanks.
In contact.html do the following:
$('#result').load('/path/to/open.php #ticketForm');
that will retrieve the form with id ticketForm from open.php and place it inside the div with id result in your contact.html page.
See the Loading page fragments section of .load() jquery method.
UPDATE:
You should put the above code in the document.ready handler inside the <body> of your contact.html, like so:
<script type="text/javascript">
$(document).ready(function() {
$('#result').load('/path/to/open.php #ticketForm');
});
</script>

php code for checking whether a div is clicked or not

Can anyone help me .I need php code for checking whether a div is clicked or not.
like,
if (isset($_POST['Submit1'])) { }
But instead of submit button i need a div ...
anyone help me please
thanks
You will need JavaScript for this as PHP doesn't access the DOM if I'm reading your question correctly. I would recommend you add jQuery lib to your page as it's simpler to add click event to a DIV, otherwise you have to add an event listener in javascript yourself for click events on the DIV.
jQuery: http://api.jquery.com/click/
DIY: http://www.codingforums.com/archive/index.php/t-122993.html
I can't tell if you want a div to be treated as a form element, like a checkbox, or if you want to use a div to submit a form.
Using it as a form element has been explained by Stano, if you want the div to submit a form, you really should just use a submit button. If you want to use an image instead of a button, using <input type="image"...> will function as a submit button.
If you really need to use a div to submit the form, you will need javascript. Something like this, we take a form named "bald" and when you click the div, the form "bald" is submitted as if you pressed a submit button.
<form name="bald" action="somefile.php">
<div onclick="bald.submit();">Click here to submit</div>
</form>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
PHP can't tell you if a div has been clicked as php only works on the Server. For this you need javascript.
jquery makes something like this very simple.
Can't quite imagine what can be this test good for, but you can use javascript like this:
<div id="clickable_div" onclick="document.getElementById('divclicked').value='YES'"> SOMETHING </div>
in your form
<input type="hidden" id="divclicked" name="divclicked" value="NO"/>
and in php
if ($_POST['divclicked']=='YES') {...} else {...}

How to upload and read text/csv file without submitting?

I have this form and I would like to read the uploaded file and then fill out the form using this read information without refreshing the page.
For example the first word might be "Bob" and so I would want that to go in my input text "First_name." I've been trying to searching online for a way to do this using JQuery or Ajax but I can't seem to find a solution.
Can this be done using the two methods previously mentioned? If so and if not can someone point me to a link or to where I can learn how to do this? The instances I have found include where one uses JQuery to upload the file and display the size without refresh (which is not exactly what I want).
I have also found how one can use an iFrame but this again is not what I want. I suppose I could always just submit the part of the page containing the textfile related information and show the same form but with the filled out information. But I feel as if this is kind of sloppy and I want to know if there is a better way.
Thanks.
Firefox has a method to do this, the File and FileList API provide a way to get at the files selected by a file input element and have a text retrieval method.
A very basic example:
NB. Not all browsers support this code.
[I think Chrome, Firefox and Opera do at time of writing.]
HTML:
<form>
<input type="file" name="thefile" id="thefile" />
</form>
<div id="text"></div>
JS (using jQuery):
$(document).ready(function() {
$('#thefile').change(function(e) {
if (e.target.files != undefined) {
var reader = new FileReader();
reader.onload = function(e) {
$('#text').text(e.target.result);
};
reader.readAsText(e.target.files.item(0));
}
return false;
});
});
Demo: http://jsfiddle.net/FSc8y/2/
If the selected file was a CSV file, you could then process it directly in javascript.
.split() will be useful in that case to split lines and then fields.
the only way I know would be to submit the form to a hidden iframe. this will upload teh file without refreshing the page. you can then use any returned info using javascript. this is what they use for fake ajax style image uploads that let you preview an image before uploading. the truth is it already has been uploaded via a hidden iframe. unfortunately however iframes are not xhtml 1.0 compliant.
something like this article may help:
http://djpate.com/2009/05/24/form-submit-via-hidden-iframe-aka-fake-ajax/
The question you might ask is :
why should I use this method instead of real ajax ?
Well they’re is numereous answer to that but one good reason it that
is doesnt require any type of ajax libs and you can start using it
even if you never used ajax before.
So here it goes.
<form method=”post” action=”formProcess.php” target=”hiddenIFrame”>
<input type=”text” name=”test” /> </form>
<iframe style=”width:0px;height:0px;border:0px;” name=hiddenIFrame />
This is just a normal form but you’ll notice the target in the form
tag, this tells the form to submit in the iframe instead of the
current page.
It’s works exactly as the target attribut on the A tag.
Also the iframe is hidden from the user using
style=”width:0px;height:0px;border:0px;”
now the file formProcess.php is not different from your normal form
processing file but if you want do something on the main page you have
to use JS like that :
window.parent.whatEverYouWannaDoInParentForm();
You can also upload file with this method !
Please checkout the formphp for full example.
Cheers !
Nb : You will see the status bar acts like the page is reloading but
it’s really not.

Using SqueezeBox and PHP in an application?

I am developing an PHP/Mysql application whereby if the user enter a certain value, the results should be displayed to him in a Mootools SqueezeBox alert.
But all the usages of Squeeze I am seeing involve link-activated alerts i.e. when someone clicks a link, the Squeezebox alert appears.
How can it be as I mentioned?
Thanks
Seems like what you want to do is feasible. Use a text input that you can add information to, then use the value set to modify the link, and click the link. Something like:
$('userinput').addEvent('change', function(evt){
var userValue = $('userinput').getProperty('value');
var url = 'http://www.example.com/?property=' + encodeURI(userValue)
$('linky').setProperty('href');
$('linky').fireEvent('click');
});
With html like:
<input type="text" name="userinput" id="userinput" />
SqueezeBox Link
Assuming SqueezeBox is adding click events, this should work.

Categories