Hi,
I am new to PHP. I wanna know how the button reacts on the click event.

This is my code snippet...

Name: <input type="text" name="fname" />
<input type='button' name='Release' onclick='hello()'
           value='Submit'>
<script type="application/javascript">
	function hello(clicked)
	{
	  alert(clicked);
	  return false;
	}
</script>

Whenever I click the button, it triggers javascript code but my requirement is, I want to print the textbox value in the same form on server side.
Your suggestions are invited....

sherry.kitchen2 commented: onclick remove +0
Member Avatar for diafol

Your suggestions are invited....

Very kind.

What do you want to do again, I don't fully understand.
Do you want to:

*print the form on the page,
*just a value on the page,
*the whole page

Why do you need php to do this? JS will handle this without sending the form to the server. Just strikes me as a strange thing to do. Could you explain what exactly is the purpose of this form and what functionality you need from the form?

You will have to use ajax to execute PHP onclick.

Member Avatar for diafol

You will have to use ajax to execute PHP onclick.

depends on what he wants php to do. Does the form have to be sent? If not - no ajax req'd. If so, the form can be printed, validated and sent to the server (via .submit) without ajax.

If php has to process before printing, ajax may be advantageous, but not essential.

BTW, I love ajax, but it can be over-used.

"Hewston, we have a noob"
I don't think you quite understand what PHP is. PHP runs on the server and once that little loading indicator has gone away their aint a whole lot PHP can do to the page. If you want a form to be submitted to the server then you don't necessary need JS at all (Tho it can be used for validation)
First can I ask this question (I will phrase it in terms of a cake shop because everybody likes caked :D)
If your server is the lady behind the counter selling cakes (The cake being the data) and the customer is the browser or client
Do you want the customer to get a cake of the lady, or the lady to give a cake to the customer?
If you gave us this piece of information then we might be able to give you more help

<script type="application/javascript">
	function hello(clicked)
	{
	  alert(clicked);
	  return false;
	}
</script>

<form action="<?php echo $_POST['fname']; ?>" method="POST">
<input type="text" name="fname" />
<input type='submit' name='Release' onSubmit="hello(document.getElementsByName('fname'))'' value='Submit'>
</form>

Does that work for you? =) I'm not that old in PHP either... But I guess this should give the basic idea at least, if it isn't working... =)

Of corse, this file should be saved as a ".php"... =)

<script type="application/javascript">
	function hello(clicked)
	{
	  alert(clicked);
	  return false;
	}
</script>

<form action="<?php echo $_POST['fname']; ?>" method="POST">
<input type="text" name="fname" />
<input type='submit' name='Release' onSubmit="hello(document.getElementsByName('fname'))'' value='Submit'>
</form>

Does that work for you? =) I'm not that old in PHP either... But I guess this should give the basic idea at least, if it isn't working... =)

Of corse, this file should be saved as a ".php"... =)

That is all so mangled, I don't know where to start.
You just can't arbitrarily sprinkle javascript here and there, for example.
Take a look at the Javascript tutorial on W3 to get a better idea how things are done.
Then peruse the PHP tutorial, or vise versa.

That is all so mangled, I don't know where to start.
You just can't arbitrarily sprinkle javascript here and there, for example.
Take a look at the Javascript tutorial on W3 to get a better idea how things are done.
Then peruse the PHP tutorial, or vise versa.

Well... Than JavaScript should be separated in an individual file... That worked for me...

Well... Than JavaScript should be separated in an individual file... That worked for me...

Yes, that is a possibility, but usually that's done if the JS is extensive, rather than clutter up the code and keep it down to a smaller file.
My point was that the js lives between the script tags and the functions are called
externally. Having the DOM node passed as an argument is strange.

<script type="application/javascript">
	function hello(clicked)
	{
          var retieve = document.getElementsByName(clicked);
	  alert(retrieve.length);
	  return false;
	}
</script>

<form action="<?php echo $_POST['fname']; ?>" method="POST">
<input type="text" name="fname" />
<input type='submit' name='Release' onSubmit="javascript:hello('fname')'' value='Submit'>
</form>

All this will do is return the number of named elements that exist.
However, by just passing the element name of interest, other DOM nodes can be extracted as well within the function, if desired.
I don't understand what the intent of the original code was, so i just did this to clarify my point a bit.

<?php
echo "<script>alert('".$val."');</script>";
?>

to have JS print the value in the alert:

<script type="application/javascript">
	function hello(clicked)
	{
          var retieve = document.getElementsByName(clicked);
	  alert(retrieve.value);
	  return false;
	}
</script>

<form action="<?php echo $_POST['fname']; ?>" method="POST">
<input type="text" name="fname" />
<input type='submit' name='Release' onSubmit="javascript<b></b>:hello('fname')'' value='Submit'>
</form>

If you use multiple lines with the same name, JS will return an array.

OK, Seriously...make the page in php...e.g. index.php
Use HTML for the form, and set the action to the page name...e.g. index.php and the method to post...
Let me go ahead and verify: in PHP, White Space doesn't count, because there are terminators...
As Shown Below...

<form method='post' action='index.php'>
<input type='text' name='fname'>
<input type='submit' value='Submit'>
</form>
<?php
$fnameval = $_POST['fname'];
echo "$fnameval";
?>

That is the only code required to send the inputed text to the server.

To Display Text from the server that was recently posted, do this...

<form method='post' action='index.php'>
<input type='text' name='fname' value="<?php 
$fnameval = $_POST['fname'];
echo 'Place Text/Variable Value Here...$fnameval'; 
?>">
<input type='submit' value='Submit'>
</form>

People Just Love to make Web Development Extremely Complicated...there is always an easy way with PHP...

I did forget one thing...to show it in a message,

<form method='post' action='index.php'>
<input type='text' name='fname'>
<input type='submit' value='Submit'>
</form>
<?php 
$fnameval = $_POST['fname'];
echo "
<script language='javascript'>
window.alert('$fname');
</script>"; 
?>

OK, Seriously...make the page in php...e.g. index.php
Use HTML for the form, and set the action to the page name...e.g. index.php and the method to post...
Let me go ahead and verify: in PHP, White Space doesn't count, because there are terminators...
As Shown Below...

<form method='post' action='index.php'>
<input type='text' name='fname'>
<input type='submit' value='Submit'>
</form>
<?php
$fnameval = $_POST['fname'];
echo "$fnameval";
?>

That is the only code required to send the inputed text to the server.

To Display Text from the server that was recently posted, do this...

<form method='post' action='index.php'>
<input type='text' name='fname' value="<?php 
$fnameval = $_POST['fname'];
echo 'Place Text/Variable Value Here...$fnameval'; 
?>">
<input type='submit' value='Submit'>
</form>

People Just Love to make Web Development Extremely Complicated...there is always an easy way with PHP...

I did forget one thing...to show it in a message,

<form method='post' action='index.php'>
<input type='text' name='fname'>
<input type='submit' value='Submit'>
</form>
<?php 
$fnameval = $_POST['fname'];
echo "
<script language='javascript'>
window.alert('$fname');
</script>"; 
?>

or, why not go with an overlay

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script src="http://cdn.jquerytools.org/1.1.2/jquery.tools.min.js"></script>
<script type="text/javascript" language="javascript">
var popStat = 0;
$(document).ready(function(){
    $("#button").click(function(){
        loadPopup();
    });
    $("#bgPop").click(function(){
        if(popStat==1){
            $("#bgPop").fadeOut("fast");
            $("#popupDisplay").hide();
            popStat = 0;
        }
    });
    $(document).keypress(function(e){
        if(e.keyCode==27 && popStat==1){
            disablePopup();
        }
    });

});

function loadPopup(){
    if(popStat==0){
        $("#bgPop").css({
            "opacity": "0.6"
        });
        $("#bgPop").fadeIn("slow");
        popStat = 1;
        setTimeout('$("#popupDisplay").show();', 500);
    }
}
</script>
<style type="text/css">
#bgPop{
        display:none;
        position:fixed;
        _position:absolute; /* for ie 6*/
        height:100%;
        width:100%;
        top:0px;
        left:0px;
        background:#000000;
        z-index:1;
    }
#popupDisplay{
        text-align:center;
        vertical-align:middle;
        display:none;
        height:100px;
        width:200px;
        background-color:grey;
        z-index:2;
        font-size:24px;
        font-weight:bold;
        margin:auto;
    }
</style>
</head>
<body>
<input type="text" id="txtText" /><br />
<input type="button" name="btnClick" value="Display Text Value" onClick="document.getElementById('popupDisplay').innerHTML = document.getElementById('txtText').value; loadPopup();" />
<div id="popupDisplay"></div>
<div id="bgPop"></div>
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.