I'm a newbee
I've got a little problem with my php-script. I try to generate dynamic formulars with php. which works very good. the problem is, I want my data to be send to another funcion in another php-script. I gues its a problem with the syntax:
<?php .... <form action=\"<?php myfunction() ?>\" ... > ... ?>
When I used it in a normal html site, it works fine:
<html> .... <form action="<?php myfunction() ?>" ... > ... </html>
So I'm lokking for a way to bring it in my php-script.
when I try this:
<?php .... <form action=\"" . myfunction() . "\" ... > ... ?>
the value won't be given to my 2nd php script
hope u have an idea, thank u guys, mirrow
If you want to pass form data to function in another script, you must just call that script from the form:
<form action="secondscript.php">
And find a way to call your function, like:
secondscript.php:
...
if ( isset( $_GET['somefield'] ) ) myfunction();
function myfunction()
{
// do something with form data
}
...
I believe, what you want is impossible! You can't send form data directly to a php function.
You need something like this:
<form action="receiving.php" method="post"><!-- or method="get" -->
<!-- your form code -->
</form>
Than you need the php of the name in the action attribute:
<?php
function your_form_processing_function($_POST) { // or $_GET
// process data
}
?>
a form's action determines the URL to which the form's data is sent to.
if you want to send your data to another page, the action field should be a path to that php page. if you want it to go to the same page, you can add a hidden field describing what function to go to, and send it to a page that routes the form to the given function based on that field.
Use this format:
<?php
.....
?>
<form action="<?php myfunction() ?>">
...
...
</form>
<?php
....
?>
Related
This is the action in my form
action="/Classes/Controllers/DoctorController.php?action=editVC"
the action I am sending is ?action=editVC
I need to also send a variable I got from this block of code
<?php
if(isset($_GET['userID'])){
$currentUserID = $_GET['userID'];
}
?>
Given that they are all in the same file, and I want to send the variable $surrentUserID like this ?userID=$currentUserID in the url as well along with the action.
I have tried this way but it did not work
action="/Classes/Controllers/DoctorController.php?action=editVC&userID=$currentUserID"
It looks like you just need to invoke PHP and print the variable, so either in your HTML:
<form action="/Classes/Controllers/DoctorController.php?action=editVC&userID=<?php print $currentUserId; ?>">
Or you might prefer to concatenate the action in a string first like:
PHP:
<?php
$action = '/controllers/DoctorController.php?action=editVC&userID='.$currentUserId';
?>
HTML:
<form action="<?php print $action; ?>">...</form>
Okay so I have an html form in Add.html. When I click submit, I would like the data to be added to my database via php and then return to the same form with "instance added" or "failed blah blah."
The only way I know how is to set the form action to a separate php file and call that - but then the php file renders and I do not return to the same form.
I would like to not have to add a "return to form" button and would prefer to return to the form on submit with a status message.
Any better ways to do this?
A very simple way to do is to do following :
yourpage.php
<?php
if(isset($_POST)){
//data posted , save it to the database
//display message etc
}
?>
<form method="post" action="yourpage.php" >....
You can do a redirect in php, to the html form - and you can set a "flash message" - to show "instance added" by saving "instance added" to the session and showing that value when you redirect to html.
you can use this trick
<?php if (!isset $_POST['Nameofyourinput']){
?>
<form method="post" action="add.html">
// your inputs here along with the rest of html
</form>
<?php
}
else
{
// Update you database and do your things here
//in your request variable you can add the error you want if things didn't go well, for example
$result = mysqli_query($connection, $sql) or die('Instance not added !'.$req.'<br>'.mysql_error());
// and then
echo (" instance added")
};
The action attribute will default to the current URL. It is the most reliable and easiest way to say "submit the form to the same place it came from".
Just give nothing to the action attribute. It will refer to your current page.
<form method="post" action="">
Other way to do this are:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Or just add '#'
<from method="post" action="#">
To handle php code. Write your code inside it.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// write your code here.
}
You should change your file extension from .html to .php .
Well you can employ old school AJAX. For instance,let's say we have a form that takes in a number N,and once we click the calculate button we should see the result the of 2^N displayed on the same page without the page being refreshed and the previous contents remaining in the same place. Here's the code
<html>
<head>
<title> Simple Math Example</title>
<script type="text/javascript">
var request = new XMLHttpRequest();
function createAjaxObject(){
request.onreadystatechange = applyChange;
request.open("POST","calculate.php",true);
request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
request.send("N="+document.getElementById('N').value);
}
function applyChange(){
if(request.status == 200 && request.readyState == 4){
document.getElementById('resultSpace').innerHTML = request.responseText;
}
}
</script>
</head>
<body>
<fieldset>
<legend>Enter N to get the value of 2<sup>N</sup> ::: </legend>
<input type="text" name = "N" id = "N">
<br>
<input type="button" value = "Calculate" onclick="createAjaxObject()">
</fieldset>
<div id="resultSpace">
</div>
</body>
The file calculate.php is the same file with the above code. When the calculate button is clicked, it calls a function createAjaxObject which takes in a value N and sends the value to the same file via the POST method. Once the calculation is done, a response will be sent. And if the response is successful, it will be sent to a function called applyChange which will render it to the same page via JavaScript.
I would like to conditionally execute the form.
I mean, "if something" then the code will be written like this and will be executed <form action="aaaa.php">. "Else", there will be <form action="bbb.php">, which will be again executed.
Execution and, of course, validation (or rewriting the action="") will be done automatically when the user press the send button.
Is it possible to do it by PHP?
Would anyone know how to make this?
Thanks a lot
Yes. It is possible. Try with -
<form action="<?php echo (something) ? 'aaaa.php' : 'bbb.php';?>" >
Check Here
yes you can code like this
<?php
if(true) { // your condition in true's place
?>
if true your html code here
like <form action="aa.html"></form>
<?php
} else {
?>
else your html code here
like <form action="bb.html"></form>
<?php
}
?>
I am trying to create a little php programme where I enter text into a text field. I then want the text to be added to an array. I do this by calling a function when the 'Add' button is clicked. After the button is clicked, I want the array entries to be displayed and the number of entries too. Here is the code I have so far, but it doesnt work :P
I have tried a few things, but nothing seemed to work.
I changed it to this now:
<form action="array.php" method="POST">
<fieldset>
<legend>Enter text here</legend>
<p>text: <input type="text" size="60" name="text"></p>
<p><input type='button' name='add' value='Add' onclick= "<?php add() ?>"></p>
</fieldset>
</form>
<?php
global $array;
$array = array();
function add()
{
if(isset($_POST['text']))
{
array_push($array, $_POST['text']);
}
return $array;
}
$arraystring = implode(", " , $array);
echo $arraystring;
?>
You have a couple of problems here. First:
<script>
function add_script(){
alert("<?PHP add(); ?>");
}
</script>
The alert does not make sense here. You cannot pass data between javascript and php like that. What you would need is AJAX. The easiest solution would be Jquery's ajax function. So you would need to set up a function to handle the data, a separate php script to receive it and process it and your safest bet is to return a JSON string which the initial script would process, read and return the data.
The second very big problem is the add() function - a function may not return more than one value. Whenever the interpreter sees
return $x;
The function will stop there and return whatever you've given it. The second will be ignored as the function has been terminated.
Your second option would be to post the entire form to the same script
<form method="post">
<!--the form inputs here-->
</form>
and when the "Add" button is clicked it will post the entire data to itself. and then in the PHP script you would need to add a condition to handle the case:
<?php
if($_POST && $_POST['text']) echo $_POST['text'];
?>
I have a populated dropdown list from mysql on one page.
What code should i write in php so that on selection of dropdown list's value, a new page is opened and data is fetched from mysql, provided i have required fields in database.
<form name="form1">
<select>
<?php
$con=mysql_connect("localhost","FreeUser","123456");
if(!$con)
{
die('Connection failed' . mysql_error());
}
mysql_select_db("human_resource", $con);
$sql1=mysql_query("SELECT eid_emp,name_emp FROM employee_details");
while ($data=mysql_fetch_assoc($sql1))
{
?>
<option name="drop1" checked value ="<?php echo $data['eid_emp'] ?>" >
<?php echo $data['name_emp']; ?>
</option>
<?php
}
mysql_close($con);
?>
</select>
</form>
The answer is there is no PHP code to do what you're asking.
You'll need two things: 1). Some javascript to hook into your form and 2). a way to render the resulting query.
here is some javascript to hook into the form, assuming jquery for brevity (although you should just give the select an ID or a class, which would make the selector less obnoxious):
$('form[name="form1"] select').change(function(){
location.href='renderer.php?dataKey=' + $(this).val();
});
From there, you'll navigate to renderer.php along with $_GET value for dataKey. render it as your will. If you want to open a new window, use a window.open call instead of setting location.href.
has nothing to do with php or mysql ... you could add an "onchange" handler to your < select > element ...
<select onchange="javascript:someFunctionCallHere()">
a call to your forms submit() method should be what you want...
Not a lot of information, but you could do something like this:
<form name="form1" method="POST" action="page.php">
<select onchange="form1.submit()">
Then in the head of your page
<?php
if(count($_POST))
{
// do stuff
header( 'Location: http://somesite.com' ) ;