Form calls 2 php functions - php

I have an html form which uses a PHP file to submit data by email. I want to add some code (which I already have) to generate random numbers for spam protection. Can I call another PHP file within my form?
Here is the code that goes in the form:
<form name="mail" action="go.php" method="post" onsubmit="return CheckData()">
<input type="text" name="q">
<input type="submit" name="Submit" value="OK">
</form>
I am a real novice with PHP so any help would be appreciated.
LozFromOz

you can do it with image in your form that call to php file.
the famous is to use captcha,
read this link :
Stopping scripters from slamming your website hundreds of times a second
a good captcha to insert in php :
http://recaptcha.net/plugins/php/

There's no need to have the browser make two http requests for two different urls to the webserver. Your php script go.php can do what ever you want it to do, e.g. include two other scripts and/or calling two functions or ...
<?php // go.php
require_once 'spam_protection.php';
require_once 'form_helper.php';
require_once 'database_something.php';
....

Related

HTML and PHP in one file

I'm a PHP newbie trying to sort some basics out. I have a user-form that leads to a mysql select query, which works fine. Every tutorial I have found so far has the standard form tag, ie: action='script.php' method='post'. This obviously opens script.php in a new tab/window though.
If I don't want to display what's fetched from my db on a different webpage I have to put the html and php in one document together. I didn't think this is how you would really want to do it though.
My specific question is when you want to display stuff on the same page do you just put everything in together within one document and let users hit the submit button?
NO you dont put your php scripts on the same page as your html file/s
Try this link for your reference =)
OR you can put 2 different pages that act as 1 by using INCLUDE FUNCTION
script1.php
<form action="script2.php" method="post" name="myform">
...
<input type="submit" name='submit_button' value="Submit" />
<input
</form>
---------------
script2.php
include 'script1.php';
if(isset($_POST['submit_button']
{.......}
Yeah You can put html and php in single document.
With the help of action.But it not the proper way.
In action you should mention this for writing html and php in same page.
<?php echo htmlspecialchars ($_SERVER["PHP_SELF"]);?>
You can use the same page as Action in form and make condition based on your submit button whthere it is pressed or not.
If it is pressed you can make your Code there for connecting db and do operation like select, insert, update or delete.
e.g.
Your file: script.php
<?php
if(isset($_POST['btnsubmit'])) {
// Do your Operation here...
}
?>
<form action="script.php" method="post" name="myform">
...
<input type="submit" name="btnsubmit" value="Submit" />
<input
</form>
What you can do is simply refer the user back to the form, or another page on your server with the header tag. Inside your PHP script you'd add something similar after your query executes correctly
header( 'Location: ' . $_SERVER['HTTP_REFERER'] ); // Refer to the last page user was on...
Or another URI
header( 'Location: http://some.url/' );
If you really want to do this, here is a way:
<?php
if(isset($_POST)){
//do your php work here
}
?>
<html>
<form method='POST'>
//form elements here
<input type='submit'>
</form>
<!-- other html code -->
</html>
It depends on the length of your code, if the code is too much, then the better way is to include some script file to your parent file. using include() functions, and your perfect answer is yes. just put everything in together within one document

Page selection based on submit buttons

In native PHP I can include a javascript code to change the action of a form sent in case I need to direct the user to which page he selects to go like this
<form action="change.php" method="post" name="form">
<input type="submit" value="Click to Page1" onclick="form.action='page1.php';return true;"/>
<input type="submit" value="Click to Page2" onclick="form.action='page2.php';return true;"/>
</form>
I would like to do the same in case I must use codeigniter or cakephp. Someone could help me with this problem ?
CodeIgniter is a backend technology. What you're writing is front end. You're pretty much all set; there isn't really much for you to change. You could, theoretically use CI's form helper, but it's unnecessary...personally, I never use it.
Unless you've removed the index.php file, change the form.action from page1.php and page2.php to index.php/mycontroller/myfunction.
The whole form idea though is sort of flawed; you don't really need it. Why not just use:
onclick="window.location.replace('index.php/mycontroller/myfunction');"
Then you can remove the form all together.

how to Pass Parameters to php page without having to load it

how can i pass parameters from an html page(map.html) to a php(createxml.php) without having to open the php page? Im incorporating googlemaps in html page(map.html) so i want the users to enter data on a form on the html page which will be sent to php(createxml.php) which in turn will connect to mySQL DB and create an xml format of the response the html page uses this xml output to create positions on the map since it contains longitude and latitude.
I have used the following code in the heading of the php page(createxml), but it shows the contents of php file for a brief moment redirecting me to map.html
Thanks for your time, i can post all the code if needed.
<meta http-equiv="refresh" content="0;url=http://localhost/map.html/">
It's quite simple with AJAX, using jQuery you don't have to know much about it :)
So simply import the latest jQuery Library.
Then you have your form:
<form id="my_form">
<input type="text" name="param1" />
<input type="text" name="param2" />
<input type="hidden" name="action" value="do_stuff" />
<input type="submit" value="Submit" />
</form>
and somewhere beneath that, you just paste this tiny javascript-function, which handles the submit of the form:
<script>
$('#my_form').submit(function(){
var post_params = $('#my_form').serialize();
$('#waiting').show();
$.post('the_page_you_are_on.php', post_params, function(data) {
$('#waiting').hide();
return false;
})
});
</script>
(The element (div, p...) with the id "waiting" could e.g. contain one of those fancy ajax loading images, but is not neccessary! :) If you want one to be shown, find one via google, set it as the background image of the #waiting-element and set its display to none (CSS)).
The function itself just calls the page you're on and then you've got the form variables in your post-array, so the top of your page could look something like this:
<?php
if(isset($_POST['action'])) {
switch($_POST['action']) {
case 'do_stuff' :
$param1 = $_POST['param1'];
$param2 = $_POST['param2'];
//do some DB-stuff etc.
break;
}
}
?>
I hope that helps!
It's a terrible idea, but because you don't want to use AJAX you could put the PHP in a frame and reload just that portion. Again, awful idea, but the closest you're going to get without using AJAX.
On a useful note though, AJAX is literally just one function in javascript. It's not hard at all to learn.
If you are just trying to pass parameters to a PHP page from the web browser, there are other ways to do it beyond 'Ajax'. Take a look at this page and view the source code (be sure to view the source of the included javascript file: http://hazlo.co/showlist.php?s=chrome&i=4e289d078b0f76b750000627&n=TODO
It uses an extremely basic method of changing the src of an image element, but passes information to the web server (PHP page) in the querystring of the image request. In this example I actually care about the results, which are represented as an image, but it sounds like you are just trying to pass data to the server, so you can return a 1 pixel image if you like. BTW, don't be fooled by the URL that is being used, a server rule is telling apache to process a specific PHP file when check it,GIF is requested.
You should play with it and use firebug or chrome's built in debugger to watch the requests that are being sent to the server.
You can't get any results from a PHP-script if you don't request it and process the output. If you dont't want to leave the current page, you have to use AJAX!
"but it shows the contents of php file for a brief moment" The reason is, that your browser first needs to load the entire page, then start the META-redirect. You don't need a redirect to load data from the server, but if you really want to, you should HTTP-headers for redirect.
Ok guys after hours of headache i finally found the solution! Basically i called my xmlproduce.php from inside my map.html, lemme explain so maybe will help others:
maps.html contained a googlmap API Javascript function which called my createxml.php called second.php
GDownloadUrl("second.php", function(data) )
what i did was i tweaked this call to second.php by adding some parameters in the URL like:
GDownloadUrl("second.php?strt="+ysdate+"/"+msdate+"/"+dsdate+"&end="+yedate+" /"+medate+"/"+dedate+"&id="+ide, function(data)
which is sending the parameters needed by second.php, so after that i made a small html form which called the script of googlemap api on the same file(map.html) to pass the parameters from the form to the GDownloadUrl i mentioned above, the code of the form is :
form method="get" action="">
IMEI: <input type="text" id="id" name="id" size="25" /> <br />
Start Date: <input type="text" id="ysdate" name="ysdate" size="4" value="2000" /> <input type="text" id="msdate" name="ysdate" size="1" /> <input type="text" id="dsdate" name="dsdate" size="1" /> <br/>
End Date: <input type="text" id="yedate" name="yedate" size="4" /> <input type="text" id="medate" name="ysdate" size="1" /> <input type="text" id="dedate" name="dedate" size="1" /> <br/>
<input type="button" value="submit" onClick="load()" />
</form>
afterwards i put extra constraints on the form for the values allowed.
Thanks everybody for the help, and you can always ask if somebody needs some clarification.

Is it possible to send variables from HTML to another PHP file

I have a index.html where I would like to submit some coordinates that can be passed upon to separate PHP file; where it could perform a query. I am new to this.
HTML:
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
PHP query:
$query = "SELECT * FROM state WHERE LONG_HI<$_POST["Ymax"] AND LAT_HI<$_POST["Xmax"];
$result = mysql_query($query);
So is there a way to perform remote action from this HTML file to the specified PHP file?
Well, Forms can do the job. Is'nt it?
Yes
Either make an HTML form to accept the Xmax and Ymax parameters, and set the form action to the PHP file;
Or use AJAX to pass the data in the background and receive a response.
If both of these concepts are foreign to you, and you don't know JavaScript, get comfortable with the first option first.
Would you please describe in detail what you are about to do?
do you have a html form?
What kind of request do you do, clicking a link, sending the form?
The query does not contain any of the variables...
could you please post excerpts of the code? single lines are useless in most cases.
Regards,
Mario
use action attribute in FORM element to specify where the request will be sent to.
<form action="another.php" method="POST">
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
</form>
You just add few line with your code because to transfer any variable value from one form to another page we have to use 'form' method. So, we have to add form tag with your code. Transferring of data from one page to another page (any type of page like php, jsp, aspx etc) is done by two methods mainly - one of them is Post and another one is Get.
Difference between both the method is quite simple. In Post method, data from one page to another page travels in hidden form whereas Get is basically used to transfer value by displaying it at url. Post method example: user-name and password, and Get Method: any query fired at Search Engine.
<form name="form" action="filename.php" method="POST" >
//Your Code
</form>

Need code to execute a single php script that exists on multiple servers (without leaving the calling page)

I've got an updater.php script located on several of my sites. This updater.php file is set to execute code when called from a home base (my central server).
So I'm looking to create a dashboard of sorts in which all my remote site addresses are listed, with the path to this updater.php script like so...
www.server1.com/path/updater.php
www.server2.com/path/updater.php
www.server3.com/path/updater.php
...etc (there will be lots of them)
And I'll create an interface to list those along with checkboxes beside each one, and a select all, etc
And I'm looking to create a PHP script that will iterate over the whole collection of urls in that list and execute the call to the updater.php file on each server, passing it a "version=v001" for example...
$.get("http://server1.com/path/updater.php?version=v001");
$.get("http://server2.com/path/updater.php?version=v001");
...etc
I've already set up the code in updater.php (the file that resides in all my sites) so that when it receives a request, it parses the $_GET['version'] to see what the version is and it knows which file to go get on my central server to perform the update.
I'm just looking for some clues how to create the script for this dashboard that sets it all into motion...
PS: In total, this is basically a batch updater script that executes wordpress theme updates without having to go to each site and do them individually.
It looks like you're using jQuery and that AJAX is fine to call the scripts rather than a PHP script. If that's not the case, ignore this.
When your "GO" button is clicked, use javascript to get the values of all the checked checkboxes. Loop through those, doing the $.get() thing. That should be all you need to do.
Optionally, you could catch the responses from the get() calls and update a status div to let you know whether they all ran successfully.
You might wanna check PHP's cURL extension which allows you to send request to multiple sites within your script quite easily.
If you need further assistance, let me know - I'll edit.
EDIT
You could use one of the javascript frameworks that would allow you to easily manage your AJAX calls (i.e. jQuery - imho the most stringent). Then do something similar to:
<form name="updater" id="updater" ...>
<input type="checkbox" name="server[]" value="0"/> Server 1
<input type="checkbox" name="server[]" value="1"/> Server 2
...
</form>
<script type="text/javascript">
var server_url = [
'www.server1.com/path/updater.php',
'www.server2.com/path/updater.php',
'www.server3.com/path/updater.php'
];
$('#updater').bind('submit', function() {
$('input[name="server[]"]:checked', this).each(function() {
$.get(server_url[this.value]);
});
});
</script>
Here you go!
I don't see the need for javascript. Honestly, I would just dump out a bunch of iframe tags which point to the appropriate urls if you're shaky on relying on php's abilities. Let the browser do the requests. Nice thing about the iframes is that you can have your remote scripts output "success" or error messages.
You can't use jQuery to do this alone because of the in-ability to do Cross Site Scripting.
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
// Run through each
foreach($_POST['servers'] as $serverHost)
{
// Now contact the server.
// if getting urls is disabled (which some hosts do)
// you can use CURL to connect to the server.
$result = file_get_contents($serverHost);
}
}
?>
<form method="post" >
<input type="checkbox" name="servers[]" value="http://foo1.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo2.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo3.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo4.com/updater.php" />
<input type="checkbox" name="servers[]" value="http://foo5.com/updater.php" />
<input type="submit" />
</form>

Categories