4

I want to write a php page in which there is a html form. I want to send all input (number for example) of my form to a php function (instead of a javascript function; I make this to hide my javascript function code).

How can I send input value to php function?
Is it possible to call the php function through onclick="function(param1, param2)"?
I know that javascript is a client-side language while php is server-side.

If it is possible, how can I write the return of the function in an input field?
I want to remain in my page.
Is it correct - action="#"?
My code is:

<form action="#" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" value="send"></input>
</form>

Help me in the implementation of the simple php function and in the passage of values from input to function! Thanks!

4
  • googled it? w3schools.com/php/php_forms.asp Commented Feb 24, 2013 at 18:39
  • did you search for ajax ? Commented Feb 24, 2013 at 18:40
  • To post to the same page, just use empty action i.e. action="". Commented Feb 24, 2013 at 18:47
  • @jimmy You might want to see this page w3fools.com W3 Schools (unfortunately) is not a reliable source of information Commented Feb 24, 2013 at 19:02

6 Answers 6

6

Make your action empty. You don't need to set the onclick attribute, that's only javascript. When you click your submit button, it will reload your page with input from the form. So write your PHP code at the top of the form.

<?php
if( isset($_GET['submit']) )
{
    //be sure to validate and clean your variables
    $val1 = htmlentities($_GET['val1']);
    $val2 = htmlentities($_GET['val2']);

    //then you can use them in a PHP function. 
    $result = myFunction($val1, $val2);
}
?>

<?php if( isset($result) ) echo $result; //print the result above the form ?>

<form action="" method="get">
    Inserisci number1: 
    <input type="text" name="val1" id="val1"></input>

    <?php echo "ciaoooo"; ?>

    <br></br>
    Inserisci number2:
    <input type="text" name="val2" id="val2"></input>

    <br></br>

    <input type="submit" name="submit" value="send"></input>
</form>
Sign up to request clarification or add additional context in comments.

18 Comments

@jcubic form's can use either - especially POST if they are handling sensitive info like email addresses, D.O.B etc
@tim.baker I know but he write method="get"
@jcubic I'm confused, what's wrong with him writing GET, your first comment says use GET now you're saying he wrote GET as those he shouldn't have done? Really forms should always use POST as anything in GET could be crawled by search engines
@tim.baker he use get in html form but post in php.
@NicholasPickering I solved! Thanks a lot! I edit your code, the problem was that the input button did not have the tag name="submit"!
|
2

You need to look into Ajax; Start here this is the best way to stay on the current page and be able to send inputs to php.

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
var xmlhttp;
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","gethint.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

</body>
</html>

This gets the users input on the textbox and opens the webpage gethint.php?q=ja from here the php script can do anything with $_GET['q'] and echo back to the page James, Jason....etc

3 Comments

Think that is far to complex for his needs.
In his question he says "I want to remain in my page" this to me says Ajax.
Remain in my page in the sense that after I click on my button the result is visible in an input field in the same php page.
2

This is pretty basic, just put in the php file you want to use for processing in the element.

For example

<form action="process.php" method="post">

Then in process.php you would get the form values using $_POST['name of the variable]

1 Comment

If I do this, I get: Undefined index 'name of the variable '
1

No, the action should be the name of php file. With on click you may only call JavaScript. And please be aware the hiding your code from the user undermines trust. JS runs on the browser so some trust is needed.

1 Comment

Ok @Christoph but I want to hide some formulas. If I use javascript formulas are visible through web browser.
0

You can write your php file to the action attr of form element.
At the php side you can get the form value by $_POST['element_name'].

Comments

-1

you must have read about function call . here i give you example of it.

<?php
 funtion pr($n)
{
echo $n;
 }
?>
<form action="<?php $f=$_POST['input'];pr($f);?>" method="POST">
<input name=input type=text></input>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.