how can i pass php variable to document.form.action in javascript ?

Hi all ,

I am use Dreamweaver CS5

my form is

<form id="form1" name="form1" method="post" action="page1.php?sId=<?php echo $row_rsS['sId'];?> 
&amp;yId=<?php echo $row_y['yId']; ?>">

<input name="submit" type="submit" id="submit"  onclick="return OnButton1();"/>

Open in new window



How can i pass php variable to document.form1.action in javascript for example


 
<script language="javascript" type="text/javascript">
function OnButton1()
{
		
		
    document.form1.action = "page1.php?sId=<?php echo $row_rsS['sId']; ?>&amp;yId=<?php echo $row_y
['yId']; ?>";

    document.form1.submit();             // Submit the page
    return true;
}

</script>

Open in new window

harabie2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
You can start your script with PHP and set your PHP variables using server-side logic before you begin any browser output.  Then you can use PHP to construct the Javascript.  See http://www.laprbass.com/RAY_php_and_js.php
<?php // RAY_php_and_js.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO PUT SOMETHING FROM A PHP SCRIPT INTO A JAVASCRIPT VARIABLE


date_default_timezone_set('America/Chicago');
$now = 'TODAY IS ' . date('r');

// END OF PHP
?>

<script type="text/javascript">
var phpdata = '<?php echo $now; ?>';
alert(phpdata);
</script>

<?php

// RESUME PHP
echo "Hello World!";

Open in new window

harabie2Author Commented:
Ray_Paseur Thank you for replay

I understand your example but how can i put php variable in the Action here

document.form1.action = "page1.php?sId=<?php echo $row_rsS['sId']; ?>&amp;yId=<?php echo $row_y['yId']; ?>";
Ray PaseurCommented:
Sigh... Learning PHP from Dreamweaver is not a very helpful way to go.  Dreamweaver has some of the worst and most confusing PHP code ever written.  And just because you can intermix PHP with HTML or JavaScript does not mean that it is a good practice, in fact, it is the source of security holes and unmanageable programming.  That is why you will often hear professionals talking about separating the logic from the presentation layers.

Have you tried changing method="post" to method="get" in the form tag?

Here is a little bit about HTTP theory that will help explain.  In a client-server architecture, clients make requests and servers respond.  Each request is atomic and complete, and each response is complete.  The system is inherently stateless.  Read the Wikipedia article on REST to learn more.
http://en.wikipedia.org/wiki/Representational_state_transfer

So the client makes a request to the server, probably through a human typing a URL into a browser address bar.  PHP runs on the server and it writes output (a response) to the client browser.  JavaScript runs on the browser.  JavaScript does not start running until the PHP script has completed its work.

Getting to something sort of like what you want is a multi- stage process.  Let's look at the form first.
<form id="form1" name="form1" method="post" action="page1.php?sId=<?php echo $row_rsS['sId'];?>&amp;yId=<?php echo $row_y['yId']; ?>">

The form tag contains embedded PHP that will echo some data into the HTML string as it creates the HTML string.  There are two PHP variables involved:
$row_rsS['sId']
$row_y['yId']

When this form is submitted, a POST-method request will be sent to the server.  However the page1.php script will find these variables in the $_GET array.  That's why I think you might be able to change POST to GET in the <form> tag.

Now on to this statement:
document.form1.action = "page1.php?sId=<?php echo $row_rsS['sId']; ?>&amp;yId=<?php echo $row_y['yId']; ?>";

The first stage will be to create the values you need in those two PHP variables.  The second stage will be to write the HTML string with those variables in the appropriate places.  Your JavaScript line would be echo'ed by PHP something like this (not sure about ampersand encoding):
// URLENCODE THE DATA FOR SAFE TRANSPORT
$s = urlencode($row_rsS['sId']);
$y = urlencode($row_y['yId']);

// USE HEREDOC TO CREATE THE DOCUMENT.FORM1.ACTION STATEMENT WITH EMBEDDED PHP VARIABLES
$dfa = <<<DFA
document.form1.action = "page1.php?sId=$s&yId=$y";
DFA;

echo $dfa;

The third stage would be to receive the request and process it in page1.php when the client hits the submit button.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

harabie2Author Commented:
Ray_Paseur Thank you Very much because you help me .

you learn me many things in this topic and i hope to be patient for me because  i am new learning java and php .

I want understand this section

// USE HEREDOC TO CREATE THE DOCUMENT.FORM1.ACTION STATEMENT WITH EMBEDDED PHP VARIABLES
$dfa = <<<DFA
document.form1.action = "page1.php?sId=$s&yId=$y";
DFA;

echo $dfa;


where i type this section in php or jave?

Thank you again
Ray PaseurCommented:
This is a section of PHP code.  All of the PHP functions are documented in the online man pages.  Example:
http://php.net/manual/en/function.urlencode.php

PHP string variables including HEREDOCs are documented here:
http://php.net/manual/en/language.types.string.php

This code is saying that we have two variables from an external source (probably a data base query) and we want to use them in a URL.  So we must URL-encode them.  Then we want to write the URL into the browser output stream in a javaScript call.  HEREDOC notation makes it easy to construct the document.form.action string.  The echo statement writes the string to the browser.
// URLENCODE THE DATA FOR SAFE TRANSPORT
$s = urlencode($row_rsS['sId']);
$y = urlencode($row_y['yId']);

// USE HEREDOC TO CREATE THE DOCUMENT.FORM1.ACTION STATEMENT WITH EMBEDDED PHP VARIABLES
$dfa = <<<DFA
document.form1.action = "page1.php?sId=$s&yId=$y";
DFA;

echo $dfa;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harabie2Author Commented:
Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.