Zand Php Cake Php

Posted by : Iv
Javascript var to php and mysql
I am having some error when i try to pass a variable via javascript

call to a php function.



I am trying to pass a state name to php so that it can search for all

records and pull it back with that name.



I keep getting an error saying:



Error displaying info: Alabama which tells me the value is being

passed but it is not being executing well as the query. Can anyone

assist with the code below and tell me what the problem is.













<script type="text/javascript">



function showlistingsbystate(thestate)

{

var thestate="Alabama";

document.getElementById('
resultsTable').innerHTML = '<?php

displayCarListingsbyState("'+thestate+'");?>';

}





</script>









<?

/**

 * displayCarListings - Displays the car database table in

 * a nicely formatted html table.

 */

function displayCarListingsbyState($thestate){

  global $database;





  echo $thestate;





  $q = "SELECT

carid,zipcode,cartype,year,carmodel,state,mileage,timestamp "

      ."FROM ".TBL_SUBMITTED_CARS." WHERE state = '$thestate'";

  $result = $database->query($q);

  /* Error occurred, return given name by default */

  $num_rows = mysql_numrows($result);

  if(!$result || ($num_rows < 0)){

     echo "Error displaying info" .$thestate;

     return;

  }

  if($num_rows == 0){

     echo "Database table empty";

     return;

  }

  /* Display table contents */

  echo '<img src="img/table_top.gif"  width="901" height="11" /

><table width="901" cellspacing="0" cellpadding="0">';

  echo '<tr class="headerRow"><th width=200>Make</th><th align=right

width=25><a rel="nofollow" href="#">Model</a></th><th align=right

width=20><a rel="nofollow" href="#">Year</a></th><th align=right

width=25><a rel="nofollow" href="#">Mileage</a></th><th align=right

width=25>Zipcode</th><th align=right width=25>State</th><th

align=right width=25><a rel="nofollow" href="#"><img src="img/

arr_desc.gif" border="0" width="16" height="16">&nbsp;Posted</a></th></

tr>';

  for($i=0; $i<$num_rows; $i++){

     $carid  = mysql_result($result,$i,"carid");

     $cartype = mysql_result($result,$i,"cartype");

     $carmodel = mysql_result($result,$i,"carmodel");

     $year = mysql_result($result,$i,"year");

     $state = mysql_result($result,$i,"state");

     $zipcode = mysql_result($result,$i,"zipcode");

     $mileage  = mysql_result($result,$i,"mileage");

     $time   = mysql_result($result,$i,"timestamp");



  if ( $i&1 ){

   echo '<tr onclick="alert(\'Loading Car Page\')" title="'.

$cartype.'" class="evenRow" onmouseout="this.className=\'evenRow\';"

onmouseover="this.className=\'rollOverRow\';"><td>'.$cartype.'</

td><td>'.$carmodel.'</td><td>'.$year.'</td><td>'.$mileage.'</td><td>'.

$zipcode.'</td><td>'.$state.'</td><td>'.date('Y-m-d', $time).'</td></

tr>';

  }

  else{

     echo '<tr onclick="alert(\'Loading Car Page\')" title="'.

$cartype.'" class="oddRow" onmouseout="this.className=\'oddRow\';"

onmouseover="this.className=\'rollOverRow\';"><td>'.$cartype.'</

td><td>'.$carmodel.'</td><td>'.$year.'</td><td>'.$mileage.'</td><td>'.

$zipcode.'</td><td>'.$state.'</td><td>'.date('Y-m-d', $time).'</td></

tr>';

  }

 }

  echo '</table><img src="img/table_bottom.gif"  width="901"

height="11" /><br>';

}









?>
 
 
Posted by : PatiDure
Re : Javascript var to php and mysql
Javascript code is on Your browser and PHP codein the server, so

there's no way that You can call PHP function from JS... Plus they are

totally different languages.



You must Use eg jQuery to send AJAX call from JS code to the server to

reach the PHP function.
 
 
Posted by : Gaurav Kumar
Re : Javascript var to php and mysql
Ohhh.. You can not work directly with Javascript and PHP in the way.
You require a medium for the interaction of Javascript (client Side) and PHP (At server end).

You require Ajax for this purpose
 
 
Posted by : TJ
Re : Javascript var to php and mysql
------------------------------
------------------------------------------------



To put the cart before the horse, capturing a php variable into

javascript is accomplished by embedding the php tags inside the

javascript tags, as demonstrated in this example (saved and accessed

in a webpage as an *example.php* file):



   <?php

       $php_variable = "php_string_or_value";      //  (php variable may

also come from a URL pass, form POST, or be returned from a php

function.)

   ?>



   <script type="text/javascript">

       var javascript_variable_from_php_variable = "<?php  echo

$php_variable  ?>";

       document.write (javascript_variable_from_php_variable);

   </script>



------------------------------------------------------------------------------





As for the horse, the same strategy doesn't work in reverse, though,

since javascript tags aren't functional inside php tags (at least in

my hands).  However, there is the capacity to embed javascript tags

within a *form* and, by posting, capture a javascript variable as a

hidden php variable, as in the following example which measures a

user's screen resolution:



   <form name="javascript_variable_capture" action="processing.php"

method="post">



       <script type="text/javascript">

               var screen_width = screen.width;

               var screen_height = screen.height;

               document.write ("<input type='hidden' name='screen_width'

value='"+screen_width+"'>");

               document.write ("<input type='hidden' name='screen_height'

value='"+screen_height+"'>");

       </script>



   </form>



and, of course, the submit button:



   <input type="submit" name="submit" value="enter" >



and, then, in a *processing.php* file:



   $screen_width = $_POST['screen_width'];



   $screen_height = $_POST['screen_height'];



This method requires the user to *submit* the form entries to POST.

There are ways to *auto-submit forms* suggested on the Net (e.g. <body

onload="submitForm()">) but I have not worked out the timing to insure

the embeded javascript code is acted upon before the auto-submit

code.  Perhaps one needs to start with FORMS constructed with

Javascript -- I have seen reference of such things, but have no

experience.  Luck.
 
 
Posted by : TJ
Re : Javascript var to php and mysql
I refined the code of my previous reply:



To pass a javascript variable into php, use a FORM POST with auto-

submit since JavaScript code cannot be embedded into PHP code.  Here

is an expample using an a html form with an auto-submit.  This method

requires two files, the first called as the webpage to begin the

process, and the second as the answering page to demonstrate that the

javascript variable has be captured by the server's php cache.



For example, name the first file javascript_var_to_php.htm as the

first webpage called, with the following code:





<form ID="variable_pass" name="javascript_variable_to_
php_variable"

action="variable_capture.php" method="post">



    <script type="text/javascript">



         var javascript_variable = "javascript_string_or_value";

         document.write ("<input type='hidden'

name='javascript_variable' value='"+javascript_variable+"'>");



    </script>



</form>





<script type="text/javascript">



    document.forms("variable_pass").submit()



</script>





Then, the information is passed to the php in the server and can be

demonstrated with the following file -- call it variable_capture.php

-- saved in the same folder as the first, with the following code:





<?php



    $php_variable_from_javascript_variable = $_POST

['javascript_variable'];



    function display_captured_js_var_into_php()

    {

         global $php_variable_from_javascript_variable;

         echo ("The JavaScript variable 'javascript_variable' passed

to the PHP ")

         echo ("variable '\$php_variable_from_javascript_variable' is

the string '");

         echo ($php_variable_from_javascript_variable);

         echo ("'.");

    };



    display_captured_js_var_into_php();



?>





==========================================================





To capture a PHP variable into JavaScript, on the other hand, simply

embed the PHP code into JavaScript tags.  As an example, call up the

webpage coded with a file -- call it php_var_to_js.php -- with the

following code:



<?php



    $php_variable = "php_string_or_value";



?>





<script type="text/javascript">



    var javascript_variable_from_php_variable = "<?php  echo

$php_variable  ?>";

    document.write ("The PHP variable '$php_variable' passed to the

JavaScript ") +

    document.write ("variable 'javascript_variable_from_php_variable'

is the ") +

    document.write ("string '" +

javascript_variable_from_php_variable + "'.");



</script>





==========================================================





luck,

TJ
 
 
If you have the better reply, then send it to us. We will display your reply after the approval.
Name : 
Email Id :   
Reply :