Coniditional execution of the form by php - php

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
}
?>

Related

I need to send an action and a variable to the url

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>

How to prevent html elements from being a part of response in $.ajax success response?

I am submitting an html form through ajax and printing the errors through the same .php file. My code looks a bit like
1.php
<?php
if(!cond){
echo "error1"
}else{
echo "error2"
}
?>
<form action="#">
<button> and other elements.
Now if i print $('#err').html(response) from my .js on any error it obviously prints whatever 1.php has to offer including html button and what not. My problem is I don't want these html entities in my response. I can obviously match for each and every error from .js and print them all individually but I was wondering if there is an efficient way to do this.
Well, if doing everything in a single file is a requirement, you could just check HTTP_X_REQUESTED_WITH header like this:
<?php
if(!cond){
echo "error1"
}else{
echo "error2"
}
if (!strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {
?>
<form action="#">
<button> and other elements.
<?php
}
But a better way to go is to create separate php file which will handle your errors.
You can only return an error code and handle it by javascript something like this:
in present.php
eval("x=" + response);
if(x.Err=='2')
{
//show error and ...;
}
and in service.php :
$Res[Err]=2;
echo json_encode($Res)

form process using php

for form process what's better ? (secure/fast)
Layout Form :
<form id="myForm" name="frm1" action="insert.php" method="post">
<textarea name="content" id="content" cols="35" rows="5"></textarea>
<INPUT type=submit value="submit" name=submit></form>
Insert php :
<?php
if (( 'POST' == $_SERVER['REQUEST_METHOD'])) {
//php validation code
} else {
exit();
}
?>
Or
<?php
if (!isset($_POST['submit'])) {
//php validation code
} else {
exit();
}
?>
The second one, definitely. It's more readable. and even more logical
<?php
if (isset($_POST['submit'])) { //php validation code
//do something
}
else
{
exit();
}
?>
You should generally be checking whether or not the data exists that you are going to process. Along those lines, your second method is preferred, but don't assume people are going to click your submit button.
I have a couple other notes for you while I'm at it. You should really close your <input> tag with /> at the end of it.
Also, while you can make comparisons like ('POST' == $_SERVER['REQUEST_METHOD']), writing them in that order makes little sense. Flip it around like this: ($_SERVER['REQUEST_METHOD'] == 'POST')
Speed is irrelevant here. In terms of security these two cakes of code are diferents...
if (( 'POST' == $_SERVER['REQUEST_METHOD']))
{//php validation code
}
else
{exit();}
Here you are testing if the request method of your page is post, and then you do your validations.
if (!isset($_POST['submit']))
{//php validation code
}
else
{exit();}
Here you are testing if there is a value in the post values that has the key "submit". You are assuming that a field has this name, but that is not necessarily true. You can have post values with any field named "submit".
The real security concern here are your validation tests.
if (!isset($_POST['submit']))
{//php validation code
}
else
{exit();}
Second one makes more sense to me.
The second answer is the best, because you're only checking on your submit button. The other one is checking for just a post .

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

PHP in PHP, Problem with dynamic forms

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
....
?>

Categories