Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 28th, 2005, 04:25 AM
Registered User
 
Join Date: Jan 2005
Location: klang, selangor, Malaysia.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ismnanie Send a message via Yahoo to ismnanie
Default passing javascript to php by Snib

http://p2p.wrox.com/topic.asp?TOPIC_ID=12125

<?php

    $username = "Snib";

?>
...
<script language="javascript" type="text/javascript">
    var sStr = "My name is <?php echo $username ?>.";

    document.write(sStr);
</script>
...


    This code will output this text:


    ...
    My name is Snib.
    ...

-----
i thought this code will answer my question, but when i try it, i mean the whole code, the output is looks like this one:
     .....
     My name is
     .....

$username doesn't work. can anybody help me?


  #2 (permalink)  
Old January 29th, 2005, 01:56 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Where are you getting the $username variable?

Possibly, you're trying to retrieve it from another page using the POST or GET form method. I suggest you try this instead:

$_POST['username']; //if the form method is POST
$_GET['username']; //if the form method is GET

Hope this helps,

-Snib - http://www.snibworks.com
Where will you be in 100 years?
  #3 (permalink)  
Old January 31st, 2005, 03:32 AM
Registered User
 
Join Date: Jan 2005
Location: klang, selangor, Malaysia.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ismnanie Send a message via Yahoo to ismnanie
Default

thank you for the suggestion but i'm still confusing. My form in a.php and i want to display data from database in b.php

1) my form has 3 functions in javascript. do i need to pass the functions to php before posting the result in b.php?

2) If i used POST method,how do i declare the javascript functions to php, so that i can retrieve the information that i want?in a.php or b.php?








  #4 (permalink)  
Old January 31st, 2005, 09:33 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Why exactly would you need to pass JavaScript functions to PHP? What are you trying to accomplish.

An equivalent to my code would be something like...

<script language="javascript" type="text/javascript">
    var sStr = "My name is <?php echo $_POST['username']; ?>.";

    document.write(sStr);
</script>

-Snib - http://www.snibworks.com
Where will you be in 100 years?
  #5 (permalink)  
Old February 1st, 2005, 01:09 AM
Registered User
 
Join Date: Jan 2005
Location: klang, selangor, Malaysia.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ismnanie Send a message via Yahoo to ismnanie
Default

I've tried $_POST method for one of the functions and it works on the next page!thank you.
However, i still have problem to pass the date.

On a.php:

//1st date
<SELECT name="MoSel" onchange="MonLen(YrSel, MoSel, DySel)"> </SELECT>
  -
<SELECT name="DySel"> </SELECT>
  -&nbsp;
<select name="YrSel" onChange="MonLen(YrSel, MoSel, DySel)"> </select>

<script language="JavaScript">
document.write(DropReadYMD(YrSel, MoSel, DySel));
</script>


//2nd date
<SELECT name="Mos" onchange="MonLen(Yrs, Mos, Dys)" >
</SELECT>
   -
<SELECT name="Dys"></SELECT>
   -&nbsp;
<select name="Yrs" onChange="MonLen(Yrs, Mos, Dys)" >
</select>

<script language="JavaScript">
document.write(DropReadYMDn(Yrs, Mos, Dys));
</script>


<script language = "Javascript">

function DropReadYMD(Yr, Mo, Dy) {
  var Y = + Yr.options[Yr.selectedIndex].text
  var M = Mo.selectedIndex // 0..11
  var D = Dy.selectedIndex + 1
  return Y + '-' + Lz(M+1) + '-' + Lz(D); }

function X()
{
  with (document.forms['form1']) { return DropReadYMD(Yr, Mo, Dy) }
}

function Y()
{
   with (document.forms['form1']) { return DropReadYMDn(Yr, Mo, Dy) }
}
</script>
//both of the functions will give yyyy-mm-dd format


By using hidden fields on the same form to send the values;
<input type="hidden" name="date1" readonly="X();">
<input type="hidden" name="date2" readonly="Y();">

---
On b.php

$dt1 = $_Post['date1'];
$dt2 = $_Post['date2'];
$query="SELECT * FROM tbl WHERE Date >= '$dt1' and
       Date <= '$dt2';
//Date in yyyy-mm-dd format

--

I want to retrieve the information between date1 and date2 but nothing works =(. Does anyone has an idea to solve the problem?



  #6 (permalink)  
Old February 4th, 2005, 07:07 AM
Registered User
 
Join Date: Jan 2005
Location: klang, selangor, Malaysia.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to ismnanie Send a message via Yahoo to ismnanie
Default

Thank you, snib. although you didnt give any answer, but i really appreciate your idea. i've tested the functions by using textfields, there's no output at all on b.php. then, i tried again by try-and-error. At last, i've found the solution.

-----
a.php
<input name="Submit" type="submit" value="Submit"
  class="formbutton" onClick="date1.value=X(),
  date2.value=Y();">

<input name="reset" type="reset" value="Reset"
  class="formbutton">
<br>

<input type="hidden" name="date1">
<input type="hidden" name="date2">

<script language="JavaScript">
function X()
{
      with (document.forms['form1']) { return DropReadYMD(YrSel,
      MoSel, DySel) }
 }

function Y()
{
       with (document.forms['form1']) { return DropReadYMDn(Yrs,
       Mos, Dys) }
}
</script>

----
b.php

$dt1 = $_POST["date1"];
$dt2 = $_POST["date2"];
$query="SELECT * FROM tbl1 Date BETWEEN '%$dt1' AND
             '%$dt2' ORDER BY Date";

---
i posted this solution so that it will help anyone who has a problem same like me^__^

http://wp.netscape.com/eng/mozilla/3...ok/javascript/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from PHP to Javascript oldBroh Javascript How-To 12 June 12th, 2008 11:12 PM
Passing javascript variable to html or php ashraf_gawdat Javascript How-To 3 July 12th, 2006 02:48 PM
Passing data from PHP to Javascript youngj PHP How-To 4 February 22nd, 2006 11:55 PM
Passing PHP variables to javascript wfrisch Pro PHP 10 April 5th, 2005 05:13 AM
passing php variable to javascript and vice versa onmcv Beginning PHP 0 March 24th, 2005 01:00 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.