Page 1 of 1

A Problem related to loosing its value

Posted: Thu Aug 04, 2005 5:24 pm
by mustafamisir
Hi,

I have this code (from search.php - search engine):

Code: Select all

<?php
       if(!isset($str))
              $str = "";
?>

   <script language="javascript">
	var str;
	
	function get_index(n)
	{
		for(i = 0;i < n; i++)
		{
			if(document.getElementById("ctrl_bib"+i).checked)
			{
				str = str + "," + document.getElementById("ctrl_bib"+i).value;
			} 
		}
	}
	function search_more(n) 
	{
		get_index(n);
		window.location.href = './search.php?str='+str;
	}
	function create_bib(n2)
	{
		get_index(n2);
		window.location.href = './createbib.php?current='+str;
		str = "";
	}
	</script>

I want to add some information to the string of "str", until I click the createBib button when I click the searchMore button. It works fine for the first search, but when I clicl the searchMore button it makes the value of "str", undefined (However, I want to add the new information to the tail of before information). After this search, if I search for another thing, it will make "undefined" the value of str.

How I can solve this problem?

NOTE: I konw thata it is mostly JavaScript code, but maybe there is a way with PHP:

Posted: Thu Aug 04, 2005 5:34 pm
by bokehman
if(!isset($_GET['str'])) ... ?

Posted: Fri Aug 05, 2005 12:47 am
by mustafamisir
it did not make any diffence

Posted: Fri Aug 05, 2005 1:11 am
by Burrito
you're mixing php with JS which for most intents and purposes, can't be done the way you're trying.

when you set a variable to something in php, that happens on the server side and can then be sent down to the client. Everything in JS happens on the client side and won't have any bearing on what takes place on the server UNLESS you send the information back to the server via a POST or GET variable.

ex of how to create a var in php and send it to the client for a JS function:

Code: Select all

$myString = "bob";
$jsStuff = "<script>";
$jsStuff .= "function name(){";
$jsStuff .= "alert('My Name is ".$mySTring."')";
$jsStuff .= "}";
$jsStuff .= "</script>";
echo $jsStuff;
there you've set a variable to a string and are echoing it thereby sending it down to the client.

ex of how to create a var on the client and send it back to the server (php):

Code: Select all

<script>
function sendToServer(varValue){
   document.MyForm.variable.value = varValue;
   document.MyForm.submit();
}
</script>
<form name="MyForm" method="post">
<input type="hidden" name="variable">
<input type="button" value="send to server" onClick="sendToServer('bob')">
</form>
there you are setting the value of a hidden form field and submitting the form to pass the information back to the server for handling.

you can make this happen much more transparently by using things like XMLHttp but in any case, you need to POST the data or send it using GET back to the server for it to do anything with it...

Posted: Fri Aug 05, 2005 8:53 am
by mustafamisir
I could not apply your way. Can you explain it in more detail?
I only convert the javascript code into PHP with string concatenation, but it did not work.