/    Sign up×
Community /Pin to ProfileBookmark

javascript/php/forms

i am trying to create a javascript function which is called by the onClick property and has a parameter that I can set with PHP. I can get everything to work, mostly. But i can’t get it to to submit a form with a name that is set by the parameter of the function.
<html>
<body>
<?
$item=”1235″;
?>
<form name=’1235′ action=”order.php” method=”get”>
<input type=”text” name=”p1″ value=”<? echo $item; ?>” onClick=”submitSplit(<? echo $item; ?>)”>
<input type=”button” value=”go” >
</form>
<script>
function submitSplit(test_var)
{
document.forms[test_var].submit();
}
</script>
</body>

</html>

i also tried using a javascript variable whose value had been set in PHP, like below. The writeln(item) line works, but the
submitSplit(item) function doesn’t.

<html>
<body>

$item=”1234″;
echo “<script language=”JavaScript”>n”;
echo “<!– hide from older browsersn”;

echo ” var item = “$item”;n”;
echo “// –>n”;
echo “</script>n”;

?>

<form name=’1235′ action=”order.php” method=”get”>
<input type=”text” name=”p1″ value=”<? echo $item; ?>” onClick=”submitSplit(item)”>
<input type=”button” value=”go” >
</form>
<script>
document.writeln(item);
function submitSplit(test_var){
document.forms[test_var].submit();

}
</script>
</body>

</html>

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@JonaOct 22.2004 — <i>
</i>onclick="submitSplit('&lt;?php echo $item; ?&gt;');"
Copy linkTweet thisAlerts:
@guigmcauthorOct 22.2004 — thank you for your suggestion. The problem still persists.

guicmc
Copy linkTweet thisAlerts:
@JonaOct 22.2004 — [font=trebuchet ms]What is the output?[/font]
Copy linkTweet thisAlerts:
@guigmcauthorOct 22.2004 — the site is at

http://www.envstudies.brown.edu/projects/urbangreens/test.php

the current code is

<html>

<head>

<title>Untitled Document</title>

</head>

<?

$item="1234";

echo "<script language="JavaScript">n";

echo "<!-- hide from older browsersn";

echo " var item = "$item";n";

echo "// -->n";

echo "</script>n";

$item="1235";

?>



<form name='1235' action="order.php" method="get">

<input type="text" name="p1" value="<? echo $item; ?>" onClick="submitSplit('<? echo $item; ?>')">

<input type="button" value="go" >

</form>

<script>

document.writeln(item);

function submitSplit(test_var){

document.forms[test_var].submit();

}

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@JonaOct 22.2004 — [font=trebuchet ms]The form's name must begin with a letter, otherwise JavaScript treats the variable as an array index instead of a string.[/font]
Copy linkTweet thisAlerts:
@guigmcauthorOct 22.2004 — thanks, that works! the test code functions.

however, the actual code still doesn't seem to work. i'll try and figure out why.

thanks

nick
Copy linkTweet thisAlerts:
@JonaOct 22.2004 — [font=trebuchet ms]Welcome.[/font]
×

Success!

Help @guigmc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...