0

I have embeded a button in my web that submits a search. The search value of this form is inputed from a txt file by a php code. The problem I have is that this php code its loaded only once when the webpage loads. This is a problem because the txt file is constantly changing and its content gets read only that first time. So what I want is to make the search value to be inputed when the submit button gets clicked. In other words, I'm looking to make the php code to run and fill the search value onClick.

Here's the form:

<form id="search" action="http://www.example.com/search.php" method="get" target="_blank">
<input type="hidden" name="search" value="<?php $file = "http://www.clavederock.com.ar/zararadio/CurrentSong.txt";
   $f = fopen($file, "r");
   while ( $line = fgets($f, 1000) ) {
   $line = preg_replace("/-(.*?)-/", "", $line);
   print $line;}?>"/>  
<input type="hidden" name="language" value="es"/>
<input type="image" src="img/psue.png">
</form>

Any suggestion or help will be aprecciated!! Thanks!

EDIT: I see php gets executed before the page is delivered because is server side scripting. To make a workaround this problem, is there a way to make the php code run every x seconds? Like you would make in a div:

 setInterval(function(){
 $('#ID').load('example.php');
 }, 10000);
fpolloa
  • 99
  • 4
  • 13
  • if your form action takes you to search.php, open the file there and extract the information that you need at time of the search – ROY Finley Dec 18 '12 at 00:52
  • Maybe using with Javascript, see here, not the same situation but about files: `http://stackoverflow.com/questions/750032/reading-file-contents-on-the-client-side-in-javascript-in-various-browsers` – rendon Dec 18 '12 at 00:53
  • I dont have any control whatsoever over search.php – fpolloa Dec 18 '12 at 00:54
  • if you are posting to search.php in same directory, how can you not have access? – ROY Finley Dec 18 '12 at 00:55

1 Answers1

4

You're going to have to tackle this a different way, PHP is server side scripting (it gets executed before the page is delivered) and JS is client side, which means it gets executed from when the page is delivered to any time when it is displayed.

You could try and use AJAX, and query the file and change the content in your callback.

EDIT: I think I understand your question better. You are going to have to use AJAX, and have the PHP code in the file that gets actioned. You can post the input value as a get variable, i.e. ?input=value and then get that in your PHP file. Then use AJAX to get the response back, without reloading the page.

Ojame
  • 169
  • 1
  • 1
  • 9
  • That's what I was guessing. Is there a way to accomplish what I want with JS only? – fpolloa Dec 18 '12 at 00:55
  • 1
    It's not going to be as pretty (or streamlined) as file handling is best done server side. However: http://stackoverflow.com/questions/6861180/how-can-you-read-a-file-line-by-line-in-javascript has a good method for reading files, which could be fired off on a .click event (and somehow passing the input value) – Ojame Dec 18 '12 at 01:00
  • I edited my question with another posibility... What do you think? – fpolloa Dec 18 '12 at 01:04
  • 1
    Yeah, that's essentially my earlier comment (as the initial question was onclick, not with a delay). Both are the same concept, and as long as you can handle the file in the method you wish, you should be fine. Please note, however, such constant queries and receiving a txt file that is large will result in increasing bandwidth/time for the user. – Ojame Dec 18 '12 at 01:07
  • Is there a way to make something like the thing I suggested in my EDIT? – fpolloa Dec 18 '12 at 01:27