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);
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.