Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php inside javascript code

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I've got some JS that I'm trying to have automatically run when the page loads, but I'm getting an 'object expected' error. The code also contains some PHP, which could be improperly formatted, causing the problem.

Anyone see what I'm doing wrong here?
Code:
<html>
    <head>
    <script type="text/javascript" language="JavaScript">
    <!--
    function doSubmit()
        {
        document.frmprocess.notify_url.value="[URL unfurl="true"]http://www.test.com/ipn/ipn_res.php";[/URL]
        document.frmprocess.cmd.value="_xclick";
        document.frmprocess.business.value="test@test.com";
        document.frmprocess.undefined_quantity.value="1";
<? echo    ("document.frmprocess.item_name.value='" . $title . "'");
   echo ("document.frmprocess.item_number.value='" . $agid . "'");
   echo ("document.frmprocess.amount.value='" . $price . "'"); ?>
        document.frmprocess.currency_code.value="USD";
        document.frmprocess.no_shipping.value="1";
        document.forms['frmprocess'].elements['return'].value="[URL unfurl="true"]http://www.test.com/ipn/thank_you.php";[/URL]
        document.frmprocess.no_note.value="1";
        document.frmprocess.lc.value="US";
        document.frmprocess.rm.value="1";
        document.frmprocess.method="post";
        document.frmprocess.action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr";[/URL]
        document.frmprocess.target="_blank";
        document.frmprocess.submit();
        }
        
        window.close();
    //-->
    </script>
    </head>
    <body onLoad="doSubmit();">
    <form name="frmprocess" action="" method="">
        <input type="hidden" name="notify_url" value="">
        <input type="hidden" name="cmd" value="">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="undefined_quantity" value="">
        <input type="hidden" name="item_name" value="">
        <input type="hidden" name="item_number" value="">
        <input type="hidden" name="amount" value="">
        <input type="hidden" name="no_shipping" value="">
        <input type="hidden" name="return" value="">
        <input type="hidden" name="no_note" value="">
        <input type="hidden" name="currency_code" value="">
        <input type="hidden" name="lc" value="">
        <input type="hidden" name="rm" value="">
    </form>
    </body>
    </html>
 
Here's the rundown:

1. It cannot be the PHP code, as the PHP code is evaluated on the server and never reaches the client (if the server processes PHP).
2. It can, however, be the data that the PHP code outputs.

Inspect the source of the generated page.
Troubleshoot the JavaScript with Firefox (install the web developer toolbar). IE in this context is pretty much useless.

Ask in the JavaScript forum: forum216
 
I copied your code, uploaded it to my server, went to the page, and viewed the source. This is what I saw:
Code:
<html>
    <head>
    <script type="text/javascript" language="JavaScript">
    <!--
    function doSubmit()
        {
        document.frmprocess.notify_url.value="[URL unfurl="true"]http://www.test.com/ipn/ipn_res.php";[/URL]
        document.frmprocess.cmd.value="_xclick";
        document.frmprocess.business.value="test@test.com";
        document.frmprocess.undefined_quantity.value="1";
[COLOR=red]document.frmprocess.item_name.value=''[/color][COLOR=red]document.frmprocess.item_number.value=''[/color][COLOR=red]document.frmprocess.amount.value=''[/color]     
document.frmprocess.currency_code.value="USD";
        document.frmprocess.no_shipping.value="1";
        document.forms['frmprocess'].elements['return'].value="[URL unfurl="true"]http://www.test.com/ipn/thank_you.php";[/URL]
        document.frmprocess.no_note.value="1";
        document.frmprocess.lc.value="US";
        document.frmprocess.rm.value="1";
        document.frmprocess.method="post";
        document.frmprocess.action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr";[/URL]
        document.frmprocess.target="_blank";
        document.frmprocess.submit();
        }
        
        window.close();
    //-->
    </script>
    </head>
    <body onLoad="doSubmit();">
    <form name="frmprocess" action="" method="">
        <input type="hidden" name="notify_url" value="">
        <input type="hidden" name="cmd" value="">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="undefined_quantity" value="">
        <input type="hidden" name="item_name" value="">
        <input type="hidden" name="item_number" value="">
        <input type="hidden" name="amount" value="">
        <input type="hidden" name="no_shipping" value="">
        <input type="hidden" name="return" value="">
        <input type="hidden" name="no_note" value="">
        <input type="hidden" name="currency_code" value="">
        <input type="hidden" name="lc" value="">
        <input type="hidden" name="rm" value="">
    </form>
    </body>
    </html>
Where are you getting the values? According to your code, do don't have values assigned to those variables.
$title
$agid
$price

[cheers]
Cheers!
Laura
 
You have to remember that by the time Javascript sees the script, PHP in done and gone. (PHP runs on the server, Javascript on the client).

Why don't you just use PHP to write form and Javascript to submit it.

Code:
<html>
    <head>
    <script type="text/javascript" language="JavaScript">
    <!--
    function doSubmit()
        {
        document.frmprocess.target="_blank";
        document.frmprocess.submit();
        }
        
        window.close();
    //-->
    </script>
    </head>
    <body onLoad="doSubmit();">
<?php
    $tmp = array();
    $tmp[] = '<form name="frmprocess" action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr"[/URL] method="post">';
    $tmp[] = '<input type="hidden" name="notify_url" value="[URL unfurl="true"]http://www.test.com/ipn/ipn_res.php">';[/URL]
    $tmp[] = '<input type="hidden" name="cmd" value="_xclick">';
    $tmp[] = '<input type="hidden" name="business" value="test@test.com">';
    $tmp[] = '<input type="hidden" name="undefined_quantity" value="1">';
    $tmp[] = '<input type="hidden" name="item_name" value="' . $title . '">';
    $tmp[] = '<input type="hidden" name="item_number" value="' . $agid . '">';
    $tmp[] = '<input type="hidden" name="amount" value="' . $price . '">';
    $tmp[] = '<input type="hidden" name="no_shipping" value="1">';
    $tmp[] = '<input type="hidden" name="return" value="[URL unfurl="true"]http://www.test.com/ipn/thank_you.php">';[/URL]
    $tmp[] = '<input type="hidden" name="no_note" value="1">';
    $tmp[] = '<input type="hidden" name="currency_code" value="USD">';
    $tmp[] = '<input type="hidden" name="lc" value="US">';
    $tmp[] = '<input type="hidden" name="rm" value="1">';
    $tmp[] = '</form>';
    echo implode("\n",$tmp)."\n";
?>
    </body>
    </html>

This makes your code cleaner and easier to debug.

Ken
 
According to LTeeple's result you need to add ; and \n to your php code:

Code:
<? echo    ("document.frmprocess.item_name.value='" . $title . "'[red];\n[/red]");
   echo ("document.frmprocess.item_number.value='" . $agid . "'[red];\n[/red]");
   echo ("document.frmprocess.amount.value='" . $price . "'[red];\n[/red]"); ?>

(only if you want to continue with your code, kenrbnsn's post is a good idea).
 
You might find the "here" approach useful.

Code:
echo '<form><input type"text" id="t1" name="t1"></form>';
echo "<script>";

$jsvar = "This is a js var value.";

$js = <<<END
var t = "$jsvar";
alert( t + "\\nIn an alert.");
document.write( t + "\\nWritten to the page.");
document.getElementById('t1').value = t;
END;

echo $js;


Hope it's helpful.

Good Place to "Learn More">>
 
Thanks for everyone's help so far. Here's where I'm at now. I'm now correctly getting the variables passed to the javascript, and not getting errors. However, the form does not appear to be getting submitted b/c no new window opens up...the page doesn't do anything.

Here's the code I have now, and also the source of the page after it runs.

code
Code:
<html>
    <head>
	<script type="text/javascript" language="JavaScript">
	
	function doSubmit()
		{
		document.frmprocess.notify_url.value="[URL unfurl="true"]http://www.test.com/ipn/ipn_res.php";[/URL]
		document.frmprocess.cmd.value="_xclick";
		document.frmprocess.business.value="test@test.com";
		document.frmprocess.undefined_quantity.value="1";
	    document.frmprocess.item_name.value="<? echo $title; ?>";
 		document.frmprocess.item_number.value="<? echo $agid; ?>";
	    document.frmprocess.amount.value="<? echo $price; ?>"; 
    	document.frmprocess.currency_code.value="USD";
		document.frmprocess.no_shipping.value="1";
		document.forms['frmprocess'].elements['return'].value="[URL unfurl="true"]http://www.test.com/ipn/thank_you.php";[/URL]
		document.frmprocess.no_note.value="1";
		document.frmprocess.lc.value="US";
		document.frmprocess.rm.value="1";
		document.frmprocess.method="post";
		document.frmprocess.action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr";[/URL]
		document.frmprocess.target="_blank";
		document.frmprocess.submit();
		}
		
		//window.close();
	
	</script>
	</head>
    <body onLoad="doSubmit();">
	<form name="frmprocess" action="" method="" target="">
    	<input type="hidden" name="notify_url" value="">
        <input type="hidden" name="cmd" value="">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="undefined_quantity" value="">
        <input type="hidden" name="item_name" value="">
        <input type="hidden" name="item_number" value="">
        <input type="hidden" name="amount" value="">
        <input type="hidden" name="no_shipping" value="">
        <input type="hidden" name="return" value="">
        <input type="hidden" name="no_note" value="">
        <input type="hidden" name="currency_code" value="">
        <input type="hidden" name="lc" value="">
        <input type="hidden" name="rm" value="">
    </form>
	</body>
	</html>

And the source of the page:
Code:
<?xml version="1.0" encoding="iso-8859-1"?>	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
	<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
	<head>
	<link rel="stylesheet" href="templates/sbi/css/template_css.css" type="text/css" />
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta name="robots" content="noindex, nofollow">
	</head>
	<body class="contentpane">
	    <!-- javascript to automatically submit the form -->
    <html>
    <head>
	<script type="text/javascript" language="JavaScript">
	
	function doSubmit()
		{
		document.frmprocess.notify_url.value="[URL unfurl="true"]http://www.test.com/ipn/ipn_res.php";[/URL]
		document.frmprocess.cmd.value="_xclick";
		document.frmprocess.business.value="test@test.com";
		document.frmprocess.undefined_quantity.value="1";
	    document.frmprocess.item_name.value="Indianapolis, IN June 14";
 		document.frmprocess.item_number.value="1";
	    document.frmprocess.amount.value="1.00"; 
    	document.frmprocess.currency_code.value="USD";
		document.frmprocess.no_shipping.value="1";
		document.forms['frmprocess'].elements['return'].value="[URL unfurl="true"]http://www.test.com/ipn/thank_you.php";[/URL]
		document.frmprocess.no_note.value="1";
		document.frmprocess.lc.value="US";
		document.frmprocess.rm.value="1";
		document.frmprocess.method="post";
		document.frmprocess.action="[URL unfurl="true"]https://www.paypal.com/cgi-bin/webscr";[/URL]
		document.frmprocess.target="_blank";
		document.frmprocess.submit();
		}
		
		//window.close();
	
	</script>
	</head>
    <body onLoad="doSubmit();">
	<form name="frmprocess" action="" method="" target="">
    	<input type="hidden" name="notify_url" value="">
        <input type="hidden" name="cmd" value="">
        <input type="hidden" name="business" value="">
        <input type="hidden" name="undefined_quantity" value="">
        <input type="hidden" name="item_name" value="">
        <input type="hidden" name="item_number" value="">
        <input type="hidden" name="amount" value="">
        <input type="hidden" name="no_shipping" value="">
        <input type="hidden" name="return" value="">
        <input type="hidden" name="no_note" value="">
        <input type="hidden" name="currency_code" value="">
        <input type="hidden" name="lc" value="">
        <input type="hidden" name="rm" value="">
    </form>
	</body>
	</html>
<!-- RmB end replacement block -->

	</body>
	</html>
 
I know folks really want to help. However, by now, there is not any PHP code involved anymore.
IMHO you would be best served in the JavaScript Forum216 where the JavaScript sharks hang out...
 
...perhaps it's b/c there are already html, head, and body tags on this page, and with the addition of my code, there are now two sets of all of those. Would that cause the form not to submit?

I can't get rid of the original html, head, body, etc tags b/c this page is part of something else.

Is there a way to automatically submit the form without using the body onLoad event?
 
Would invalid HTML cause a form (in an extra <body> tag) to fail? I'd say tht is a sure bet.
Maybe you should just tell us what you are trying to do.
 
Maybe you should just tell us what you are trying to do."

...see my initial post. Automatically submit this form...to PayPal, with those three variables. That's all I'm trying to do.
 
What is it you don't want to tell us?.
Your initial post does not say anything about the form, nor does it mention that the form appears not to be your form to begin with.
I can't get rid of the original html, head, body, etc tags b/c this page is part of something else.

Unless you explain what you are trying to do I just have to assume that you are up to no good and can't help you.
The way this sounds it appears to be a cross-site scripting attempt.
[peace]
 
Arghh! I just mean that there is more code to this page. But that other code isn't affecting this issue. Trust me, you don't want me to post all 1000 lines of code. Nothing to hide...it's all part of the Mambo CMS system. The page in question is a popup that accepts some form input, then when user submits the form, it runs again, this time running the code in question.

Not that I'm looking for an explanation, I don't have the time, but I don't even know what a "cross-site scripting attempt" is. Sounds like you might think I'm up to something fishy. If so, that's mighty presumtuous of you. This is all my own code, on my own site.

Anyone got any helpful ideas?
 
Allright, that helps.

Your code would porperly work when it would appear in the place where your last source code displays <!-- javascript to automatically submit the form --> and the onLoad attribute of the original HTMLs' body tag were inserted.

How are you appending the extra code to the page now?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top