PHP submit form if conditions are met - php

I'm NOT using javascript has it is disabled in my environment.
Please review the code below.
I'm trying to use PHP to create the logic that Javascript or jQuery would allow me to do with a simple : document.form2.submit()
<div>
<h2>How many services do you need ?</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST" name="fServ">
<input type="number" name="numServ" />
<input type="submit" value="SEND">
</form>
<div>
<?php
if(isset($_POST['numServ']) && $_POST['numServ']!==""){
echo "We need ".$_POST['numServ']." services";
echo "<form name='form2' id='form2' method='get' action= $_SERVER[PHP_SELF]>";
for($k = 0 ; $k< $_POST['numServ']; $k++){
//echo "<input type=\"text\" value=\"services$k\">";}
echo "<br><input type='text' name='services$k' value=''>";
if(empty($_GET['services'.$k])){
echo "You didn't fill up all the fields. Please put in a service";
}
if (!empty($_GET['services'.$k])){
echo "Form Filled";//
}
}
}
echo "<input type='submit' name='sendForm' value='SEND'/></form>";
if(!isset($_POST['numServ'])){
echo "We don't have any services yet.";
}
else if($_POST['numServ']==""){
echo "Please put in a number";
}
?>
Can this be achievied through PHP ? Where if the conditions are met and the form fields are NOT blank then submit the form, otherwise die and show a message.

Even then you need to submit the details of the form:
Name: <input type="text" name="name" /> (Min 5 Chars)
Age: <input type="text" name="age" /> (Should be a Number)
<input type="submit" />
Now using server-side validation, this can be done this way:
if (isset($_POST["name"], $_POST["age"]))
if (strlen($_POST["name"]) > 5 && is_numeric($_POST["name"]))
die("Error! The conditions are not met!");
die("Form Submitted!");
This way you can submit the form where JavaScript is disabled.

If I understand, you need auto-submit the form without javascript.
Maybe this thread helps you:
How to submit a form on page load without clicking submit button?

Related

PLS HELP PHP syntax

Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.

Click event on two buttons for if else condition is not working

Advance thanks for your help. I am not understanding why it is not working. I am a beginner and hence I am not finding the problem. Please anyone help me...
echo "<form method='POST'><br>
<p>Would you Like to create your Account with Details Information<b>?
</b>
<br><br>
<input type='button' id='yes' name='yes' value='Yes'/>
<input type='button' id='no' name='no' value='No'/></p>
</form>";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['yes'])) {
echo "Yes Clicked";
} else if (isset($_POST['no'])) {
echo "<br><br><p> You have successfully Registered!!!</p>";
}
}
Replace button with submit
<input type='submit' id='yes' name='yes' value='Yes'/>
<input type='submit' id='no' name='no' value='No'/></p>
try this code :
<?php
echo "<form method='POST' action='index.php'><br>
<p>Would you Like to create your Account with Details Information<b>?
</b>
<br><br>
<input type='submit' id='yes' name='yes' value='yes'/></p>
<input type='submit' id='no' name='no' value='No'/></p>
</form>";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['yes'])) {
echo "<br><br><p> Yes clicked!!!</p>";
} else if (isset($_POST['no'])) {
echo "<br><br><p> You have successfully Registered!!!</p>";
}
}
?>
form are trigged with input type submit , so if you want to get value of two input , they must be a submit input instead of button . in this cas , you have two input type submit , theey should be type submit with different name .that's all
You have to change your button's type attributes to submit instead of button, or else they won't actually submit the form.
<input type='submit' id='yes' name='yes' value='Yes'/>

How start a JQuery inside PHP

I want to start a jquery (freeow plugin) when I got an error in e-mail input field form.
I'm checking some fields on the form and each error that I got I want to open an alert message.
I can do this using a button, like this...
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Freeow!" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="Error message..." type="hidden" />
</div>
<div class="form-line">
<input id="freeow-show" type="button" value="Click to Freeow!" />
</div>
How do this without the button? Inside the PHP code.
if ($erro == "0") {
if(filter_var($email01, FILTER_VALIDATE_EMAIL)){
$erro = "0";
} else {
echo "<div id=\"freeow-tr\" class=\"freeow freeow-top-right\"></div> ";
echo "<div id=\"freeow-br\" class=\"freeow freeow-bottom-right\"></div> ";
echo "<div class=\"form-line-check\"> ";
echo "<input id=\"freeow-title\" value=\"Freeow!\" type=\"hidden\" class=\"text\" /> ";
echo "<input id=\"freeow-error\" value=\"0\" type=\"hidden\" /> ";
echo "<input id=\"freeow-position\" value=\"#freeow-tr\" type=\"hidden\" /> ";
echo "<input id=\"freeow-style\" value=\"smokey\" type=\"hidden\" /> ";
echo "<input id=\"freeow-message\" value=\"Error message...\" type=\"hidden\" /> ";
echo "</div> ";
$erro = "1";
}
}
You have to create a javascript function
function mensagem01() {
var title, message, opts, container;
title = $("#freeow-title").val();
message = $("#freeow-message").val();
message = 'E-mail not in database...';
opts = {};
opts.classes = [$("#freeow-style").val()];
opts.prepend = false;
opts.classes.push("error");
opts.autoHide = false;
opts.classes.push("slide");
opts.hideStyle = { opacity: 0, left: "400px" };
opts.showStyle = { opacity: 1, left: 0 };
container = $("#freeow-position").val();
$(container).freeow(title, message, opts);
}
HTML code need to have the freeow definitions
<div id="freeow-tr" class="freeow freeow-top-right"></div>
<div id="freeow-br" class="freeow freeow-bottom-right"></div>
<div class="form-line-check">
<input id="freeow-title" value="Lembrou?" type="hidden" class="text" />
<input id="freeow-error" value="0" type="hidden" />
<input id="freeow-position" value="#freeow-tr" type="hidden" />
<input id="freeow-style" value="smokey" type="hidden" />
<input id="freeow-message" value="message field must be empty" type="hidden" />
</div>
Inside PHP code
You need to include the javascript code to call the javascript function message
echo "<script type=\"text/javascript\"> $(document).write(mensagem01()); </script> \n";
When the php is executed the javascript write the document, which contains the call to the freeow message.
What you are trying to achieve will is best done with AJAX and some more logic than you have in your code.
You want to catch the form submit with jQuery. Using event.prefentDefault() you can stop the form from submitting. Then, you want to AJAX your FormData to the server.
In your PHP script, you do your validation. If it succeeds, return something (for example, a JSON object) that tells your front end that the validation was a success. If not, you want an object that describes what exactly is failing.
From front end, you check to see if the validation was a success (do whatever), or not (show freeow)

If checkbox with label is checked, submit form

I cant seem to get my php script to recognize that the checkbox in my form is checked.
I want to accomplish this:
If checkbox is checked, the php script should submit to my DB.
PHP:
<?php
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
//submit
} else {
//do nothing
}
}
?>
FORM:
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
NOTES:
Im using Jquery to keep checkbox checked during session.
Im using CSS to customize checkbox.
I dont know if these two might corrupt anything.
Hope u can help.
Your code is fine it is working
if (!empty($_POST['approve_student'])) {
if (isset($_POST['approve'])) {
echo "Approved";
} else {
echo "Not Approved";
}
}
<form action="index.php" method="post">
<input class="checkbox" name="approve" type="checkbox" id="approve">
<label name="approve" for="approve"><span><div data-textbox="1" ></div></span></label>
<input class='button_submit_2' name="approve_student" type="submit" value='Submit'>
</form>
If the checkbox isn't checked then the value shouldn't be coming through in the first place, so this should work;
if(isset($_POST["testvariabel"])){
//whatever you wanna do here
}
or if it's always coming through...
Change the markup to something like this, setting a value property
<input type="checkbox" class='form' onclick="this.value=!this.value" value=true name="checkbox[]" />
and to get the values submitted, use a simple loop
if($_POST['checkbox'] == 0){
echo $checkbox . ' ';
}

Multiple submit on form php

On my form I have 3 submit,I want to know the best way to handle these buttons because the default action is the same form:
<form method="POST" action="file_where_form_is.php">
Name:<input type='text' name='name'>
<input type='submit' name='add' value='Add Names' ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit'name='Preview'value='Preview'ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit' name='submit'value='Submit' ONCLICK='window.location.href="send.php">
</form>
This not seems to work completely,just the 2 first work and for the last submit does nothing(with name='submit')
Any suggestions?
I finally found in header part add something like this:
if(isset($_POST['submit'])){
header("location: somefile.php");
}
elseif(isset($_POST['Preview'])){
header("location: anotherfile.php");
}
elseif(isset($_POST['add'])){
header("location: anotheronefile.php");
}

Categories