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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

call on click event 2

Status
Not open for further replies.

kurie

Programmer
Jun 4, 2008
170
ZA
i would like to write an on click event on a button and im doing it like this
<form name="form1" action="" method="post">
<input type="button" name="mybutton" onclick="<?php print "my Test"; ?>";
form1.submit()">
</form>
its not working(not printing and no error message), does anyone know what i doing wrong here .
thanks
 
Onclick is a Javascript event, not PHP.

The only thing you would get out of your code is your submit button code with your text in the onclick event. (look at the source code for the page, and you'll see the text is there.

but since "my test"; is not a valid javascript function it does nothing.

Try looking at a javascript alert().


Code:
<input type="button" name="mybutton" onclick="[red]alert('my Test");[/red]
form1.submit()">




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thanks guys but cant i do something like

im mean something like this

<?php
if($Submit)
{
print "testing";
}
?>

<form method="post" action="">
<input type="Submit" name="submit" value="Submit">
</form>

this is giving me an error message:

Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\tmobile\button.php on line 2
 
You need to set

$Submit = $_POST['Submit'];

on the receiving page.

You also need to specify which page you're actioning in the form 'action'.

~Ben
"Insert witty statement here
 
Hi

Both statements are correct. But.
Ben said:
You need to set

$Submit = $_POST['Submit'];
Not doing so generates a warning :
PHP interpreter said:
Notice: Undefined variable: Submit in C:\xampp\htdocs\tmobile\button.php on line 2
So it is not related to the parser error received by kurie.
Ben said:
You also need to specify which page you're actioning in the form 'action'.
Not doing so will default to current URL in all modern browsers. I would say, it is just good for testing/planning.

Feherke.
 
Not doing so will default to current URL in all modern browsers. I would say, it is just good for testing/planning."

I didn't know that. Never actually left an action blank on a form!

It's not throwing any errors for me, so I don't know what might be causing it.

~Ben
"Insert witty statement here
 
Hi

kurie, personally I do not understand what you want or what you are trying.

Let me show you a simple example, so you have an idea why I mentioned AJAX :
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd">[/URL]
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title></title>
<script type="text/javascript">
if (typeof XMLHttpRequest=='undefined') {
  XMLHttpRequest=function() {
    try { return new ActiveXObject('Msxml2.XMLHTTP.6.0') } catch (e) { }
    try { return new ActiveXObject('Msxml2.XMLHTTP.3.0') } catch (e) { }
    try { return new ActiveXObject('Msxml2.XMLHTTP') } catch (e) { }
    try { return new ActiveXObject('Microsoft.XMLHTTP') } catch (e) { }
  }
}
var url='tadam.php'
function dophp(what)
{
  var http=new XMLHttpRequest()
  http.open('GET',url+'?text='+escape(what.value),false)
  http.send(null)
  if (http.readyState==4 && http.status==200) {
    alert('PHP\'s message to JavaScript :\n\n'+http.responseText)
  }
}
</script>
</head>
<body>
<form name="form1" action="" method="post">
<p>
<input type="button" name="mybutton" value="One" onclick="dophp(this)">
<input type="button" name="mybutton" value="Two" onclick="dophp(this)">
<input type="button" name="mybutton" value="Three" onclick="dophp(this)">
</p>
</form>
</body>
</html>
Code:
<?php
echo "I received $_GET[text], I mean ",strtoupper($_GET[text]),' !';
?>
As you can see, there is not much to do with PHP. The communication is carried by JavaScript.

As you can also see, the [tt]form[/tt] is not submitted automatically. But you can compose the [tt]GET[/tt] and/or [tt]POST[/tt] data yourself.


Feherke.
 
As feherke pointed out, the code you posted could not have generated the error you say you are getting. Something else you are not showing is causing that error.

I think AJAX maybe a bit of overkill at this point, but if you explain what it is you want to accomplish we may be able to help.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top