How to Auto Submit you Html form using PHP - php

Hi guys I have html form which is set to action = something.php. now i want to autosubmit this form 9 times when submit button on the form is clicked. can i do this using PHP. Thanks in advance.

Since PHP is a server side script, you cannot actually submit the form 9 times. You can, however, do whatever you're doing with the data on the server side 9 times in a for loop.
for($i = 0; $i < 9; $i++) {
// use $_POST here and do your stuff
}

do you want to submit the form 9 times to the same PHP script, or do you just want to handle the submission 9 times? Don't think you can do the former from PHP, but the later would be easily handled via a loop:
for ( $counter = 0; $counter < 9; $counter += 1) {
\\ do your stuff
}
Do you have a particular reason why you want to submit the form vs. just running through the logic multiple times?

No, you can't do this in PHP. At least not the way you are thinking of doing it (actual multiple POST actions from your form page). The action of posting will automatically take the user to the page you are posting to, not giving you an opportunity to POST again. You could of course POST to another script form the script you POSTed to, but I don;t think this is what you are asking.
To make actual repeated physical POSTs you would likely need to use AJAX methods.

HTML Page
<form id="form1" name="form1" method="post" action="form.php">
<input type="text" name="name" id="name" />
<input type="text" name="email" id="email" />
<input type="submit" name="button" id="button" value="Submit" />
</form>
PHP Page
<?php
for($i=1; $i<=9; $i++) {
// Your posting stuff
echo $_POST['name'];
echo '<br>';
echo $_POST['email'];
echo '<br>';
}
?>
This is the way you can use.

Related

Multiple forms using PHP engine on the same page are submitted all in one go

I have multiple inquiry forms all of which call the same file used for email forwarding, so it's titled emailForwarding.php. I apparently managed to separate the forms using jQuery on the front end, but the script in emailForwarding.php is processed the same number of times as the number of the inquiry forms. I just want the php script to work for the form I submit.
I tried isolating the effect of the script using .eq() and .index() and passing an argument named $arg to only trigger form submission event for the div.vendor-wrapper containing the selected form.
single.php:
echo
'<div class="vendor-wrapper"><form method="post" action="" name="form" class="commentForm">
<textarea name="comment" placeholder="Please enter your message in the space of 300 characters and hit the Confirm button." value="" class="message" maxlength="300"></textarea>
<input name="confirm" type="button" value="Confirm">
<input class="send" name="send'.$i++.'" type="submit" value="Send">
<input type="hidden" name="position" val="">
</form></div>;
<script>
$('.confirm').click(function(){
$('.vendor-wrapper').find('.position').val('');
var index = $(this).parents('.vendor-wrapper').index()-1;
if($('.vendor-wrapper').eq(index).find('.message').val()){
$('.vendor-wrapper').eq(index).find('.confScreen').show();
$('.vendor-wrapper').eq(index).find('.position').val(index);
}
});
</script>
emailForwarding.php:
if(isset($_POST['position'])):
$arg = 'send';
$arg .= $_POST['position'];
echo "<script>console.log('PHP: ".$arg."');</script>";
if(isset($_POST[$arg])):
if(isset($_POST['comment'])):
$EmailCustomer = $_POST['email'] = $current_user->user_email;
//The rest of the script for email processing omitted.
The form is submitted the same number of times as the number of the forms on the page.
Inserting include() before tag of single.php disabled duplicate submission.
Could you provide more code? Because I was trying to reproduce the problem but could not with the provided code. As, what $_POST['position'] stands for is not clear from code.
Is the echo statement user any loop. Can you try by giving a different name to FORM?
<form method="post" action="" name="form-$i" class="commentForm">

using array with cookie for form data

I'm using a form to post data and collect it with PHP by using individual cookies after some validation..
it takes too much time to do things with the data which gets extended for more and more forms... I was thinking about improving my code with loops, functions or arrays, offered to create a map array...
share your suggestion on how should I re-write my code for more data and multiple forms which get the data exported to documents..
<?php
if(isset($_POST['send']))
// name
if(strlen($_POST['name']) < 2){
echo "error - name is too short";
}
// other validations with elseif..
else {
setcookie('001',$_POST['name']);
}
// age
if((isset($_POST['age']) && is_numeric($_POST['age']))){
setcookie('002',$_POST['age']);
}
?>
<form method="post">
<input name="name" type="text"/>
<input name="age" type="number"/>
<input type=submit" name="send" value="submit">
</form>
I think should store it as a key val pair in $_SESSION. You can infinitely add more key-val pairs without worrying much. Also it will be secure as the data won't be exposed in cookies.

Calculating values with formulae from user input in PHP

I want to do an equation using the values I get from the input boxes called ampMin, voltMin and hastMin...
I'm not sure if it's a syntax problem or my method of approach is plain wrong.. Here's is an example of how the equation should look and work like with Excel.
What am I doing wrong? Thank you for your time!
EDIT: worth mentioning is that the whole block of code is within "strckenergi.php".
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin"> </pre>
<?php
echo "kJ/mm (minimum) = " . $qMin
$qMin = ( $ampMin * $voltMin ) / (( $hastMin * 1000 ) / $hastMin * 0.8));
?>
</body>
</html>
This works.
<html>
<title>Sträckenergi</title>
<body>
<h3>Svetsmetod: 111</h3>
<h4><i>Med K=0.8</i></h4>
<pre>
<form method="post" action="form.php">
Amp. Min <input type="text" name="ampMin"> Volt. Min <input type ="text" name="voltMin"> Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit"> </pre>
</form>
<?php
if($_POST){
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( $ampMin * $voltMin ) / ( $hastMin * 1000 ) / $hastMin * 0.8;
echo "kJ/mm (minimum) = " . $qMin;
}
?>
You don't appear to be using forms correctly. If you want the browser do be able to display it right away, you should use JavaScript instead. PHP will require an additional pageload, or an AJAX request.
And in order to post the form, you need a submit button. Otherwise, the browser won't know what to do with it.
Further, your first PHP line needs a semi-colon, and your second one needs to be above the first - otherwise, the interpreter won't know what your value is when printing it, because it hasn't been calculated yet.
To be honest, I think you need to start by googling for how to construct an HTML form, then you can look up simple JavaScripts. Lycka till!
First off, as Joel Hinz says above, you need a submit button so the page knows when the user is done inputting and wants to send the form to the server for processing.
Second, you need to close the form with a tag.
Third, you're probably better off sticking with php at this stage; JavaScript can be a bit tricky and mighty frustrating for beginners.
This is a rough approximation of how your form should look.
<form method="post" action="strckenergi.php">
Amp. Min <input type="text" name="ampMin">
Volt. Min <input type ="text" name="voltMin">
Hast. Min <input type="text" name="hastMin">
<input type="submit" value="submit">
</form>
See http://www.tizag.com/phpT/forms.php for a clear explanation of forms.
OK first thing first. When the form is submitted, the values contained in the $_POST array aren't immediately available to the script on the server which processes the input.
For that you will need something like the following:
<?php
if($_POST){// this checks for the existence of the $_POST array, i.e. was something submitted
//now we're assuming a form was submitted
$voltMin = $_POST['voltMin'];
$ampMin = $_POST['ampMin'];
$hastMin = $_POST['hastMin'];
$qMin = ( ($ampMin*$voltMin)/($hastMin*1000)/($hastMin*0.8) );
echo "kJ/mm (minimum) = " . $qMin;
}// if($_POST)...
?>
Then you can process the elements, and print out the results of your calculations.
Oh, and drop the pre tags unless you really need them.

How to populate input fields with PHP

I'm trying to write a simple calculator html page where I ask for two numbers in separate text boxes, have the user click a button and then the result gets stored into a third text box. I'm trying to using PHP to resolve this but can't seem to display it the way I want.
the echo line works fine, but I don't want that;
From HTML
<form action="Add.php" method="get">
<input for='num1' type="text" name="num1" />
<input for='num2' type="text" name="num2" />
<input type="submit" value="Add Them" />
<input type="text" name="AnswerBx" id="SumTotalTxtBx"/>
</form>
Add.php
<?php
$num1 = $_GET["num1"];
$num2 = $_GET['num2'];
$sum = $num1+$num2;
//echo "$num1 + $num2 = $sum";
document.getElementById("SumTotalTxtBx").value = $sum;
?>
You can't mix PHP and JavaScript like that! One is run on the server the other on the client.
You have to echo the value into the value attribute of the text boxes like so
<input type="text" value="<?PHP echo $sum; ?>" />
You are mixing PHP and Javascript here, go back to the PHP manual at : http://php.net/manual and read out how php works and interacts with the user.
Javascript is basicaly run on the client side while PHP is run on the server. You REQUEST php something and it returns a new HTML page for you.
That being said, its a too broad topic to help you fix your error.
Good luck
Using jQuery you can solve it without doing post back's at Server like this
var num1 = parseInt($("input:text[name='num1']"));
var num2 = parseInt($("input:text[name='num2']"));
$('input:text#SumTotalTxtBx').val(num1+num2);
Okay, so PHP is awesomeness, but all of it's calculations are performed on the serverside, not the clientside. Using AJAX, you can execute some of the PHP code back against the server, but I think you may be more interested in javascript for your calculator.
<html>
<head>
<script>
function calculateSum()
{
num1 = parseInt(document.getElementById("num1").value);
num2 = parseInt(document.getElementById("num2").value);
sum = num1 + num2;
document.getElementById("sum").innerHTML = sum;
}
</script>
<body>
<input id="num1" type="text"/><br/>
<input id="num2" type="text"/><br/>
<button onclick="calculateSum()">Submit!</button>
<span id="sum">..the sum is..</div>
</body>
</html>
Javascript is really quite simple, and is absolutely wonderful when programming anything on the clientside. If you want to learn more, I'd suggest you read up on javascript over at w3schools! I hope that helps!!

form variables by one search button?

Web site: www.matka-opas.com/ahaa4.php
If i push SEARCH-1 and then SEARCH-2 it works ok,
but how it could do by one search button ?
<?php
$depairport = $_POST['depairport'];
$ddestination = $_POST['ddestination'];
$ddday = $_POST['ddday'];
$depmonth = $_POST['depmonth'];
$depyear = $_POST['depyear'];
$retday = $_POST['retday'];
$retmonth = $_POST['retmonth'];
$retyear = $_POST['retyear'];
$aikuisia = $_POST['aikuisia'];
$osoite = $depairport . $ddestination . $ddday . $depmonth . $retday . $retmonth;
?>
<form method="post">
<input type="submit" value="SEARCH 1">
thanks ! ;]
If i push SEARCH-1 and then SEARCH-2
it works ok, but how it could do by
one search button ?
Just specify one search button and it will submit the form it is under. And you shouldn't be specifying more than one submit button for a particular form.
Here is what you should have:
<form method="post" action="">
.............
.............
.............
<input type="submit" value="SEARCH">
</form>
if you want to submit your form to http://www.travelstart.fi/combo/ you should put the url in the action attribute of the form. Your form will be posted to that url when it is submitted with <input type="submit" value="SEARCH">
<form method="post" action="http://www.travelstart.fi/combo/">
You also might want to learn more about semantic web and the separation of content and markup. I can understand you want to use tables in this kind of layout but 3 <br> to create a top margin? I saw you included a stylesheet in your page so...
EDIT: Now that your question is updated I think I understand it better. If you don't need any server-side functionality to calculate the url it might be better to put this functionality in javascript. This means it will be executed inside the browser. This saves you a round-trip to the server and so makes your page navigate a lot faster. This is a quick setup to get you started:
<form id="myForm">
...
<input type="submit" />
</form>
<script type="text/javascript">
myForm.onsubmit = function() {
window.location = 'http://www.travelstart.fi/combo/' + myForm.depairport + myForm.ddestination + myForm.ddday + myForm.depmonth + myForm.depyear + myForm.retday + myForm.retmonth + myForm.retyear + myForm.aikuisia;
return false;
}
</script>
As a disclaimer: this is not the best way to program it, it is just a short example to guide you in the right direction. Getting the right javascript code for what you want to achieve is a different question on SO.
It will probably not work right away since I don't think that will result in a valid url but I hope you get the picture. To make it cross-browser compatible you might want to use a javascript library like JQuery.
if you are trying to submit data
<input type="submit" value="SEARCH 1">
and then go to different url
to combine this you can use php that would detect a submit and redirect user to a different page...
as example:
$depairport = $_POST['depairport'];
$ddestination = $_POST['ddestination'];
$ddday = $_POST['ddday'];
$depmonth = $_POST['depmonth'];
$depyear = $_POST['depyear'];
$retday = $_POST['retday'];
$retmonth = $_POST['retmonth'];
$retyear = $_POST['retyear'];
$aikuisia = $_POST['aikuisia'];
$osoite = $depairport . $ddestination . $ddday . $depmonth . $retday . $retmonth;
if (isset($_POST['Submit'])) {
header( 'http://www.travelstart.fi/combo/'.$osoite ) ; }
you might want to change html just to:
<input type="submit" name="Submit" value="SEARCH 1">
thats all - make sure You are using POST method submiting the data

Categories