I currently have this:
function submit()
{
document.getElementById("lostpasswordform").click(); // Simulates button click
document.lostpasswordform.submit(); // Submits the form without the button
}
<body onload="submit()">
<form name="lostpasswordform" id="lostpasswordform" action="/" method="post">
<input type="hidden" name="user_login" id="user_login" class="input" value="<?php echo ($user_login); ?>" />
</form>
</body>
it works on PC, but for some reason, javascript is not execute from iPhone so I'm wondering if theres a way to auto-submit the form using PHP instead of JS?
Thanks
There's no way to trigger a form submission server-side. You'd have to use a language that works in the DOM like JavaScript for this one. From what you've given us, I don't see why it wouldn't work with the way you have it set up now.
Check your code and if it still doesn't work, I'd suggest asking this question in a different context; something along the lines of getting your JavaScript to work on the iPhone instead of dumping it altogether.
As esqew points out, you can't perform a client side action from the server. Your options are to rework your function so it doesn't need the autosubmit (maybe you could use a GET variable rather than posting) or finding a work around for the iPhone.
For a workaround - the .click() function doesn't work on iPhones. You could try one of the solutions that have come up from this problem before such as using tap or this larger touch handler function.
No, PHP cannot do that, but your problem is due to the way the iPhone handles click events. Here's background info and a workaround. It seems like all you need is an empty onclick function to trigger it, so:
// untested
var f = document.getElementById('lostpasswordform');
f.onclick = function () { };
document.lostpasswordform.submit();
You might want to think of the experience for a user though -- why would clicking inside a form automatically submit the form? What's wrong with a submit button?
Here is a minimal javascript answer from a PHP Programmer like myself:
/** This is the script that will redraw current screen and submit to bank. */
echo '<script>'."\n" ;
echo 'function serverNotifySelected()'."\n" ;
echo '{'."\n" ;
echo ' window.open(\'\', \'BankPaymentScreen\');'."\n" ;
echo ' document.forms[\'bank_form\'].submit();'."\n" ;
echo ' document.forms[\'server_responder\'].submit();'."\n" ;
echo '}'."\n" ;
echo '</script>'."\n" ;
/** This form will be opened in a new window called BankPaymentScreen. */
echo '<form action="https://www.sandbox.bank.com/cgi-bin/webscr" name="bank_form" method="post" target="BankPaymentScreen">'."\n" ;
echo '<input type="hidden" name="cmd" value="_s-xclick">'."\n" ;
echo '<input type="hidden" name="custom" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="hosted_button_id" value="'.$single_product->hosted_button_id.'">'."\n" ;
echo '<table>'."\n" ;
echo ' <tr>'."\n";
echo ' <td><input type="hidden" name="'.$single_product->hide_name_a.'" value="'.$single_product->hide_value_a.'">Local</td>'."\n" ;
echo ' </tr>'."\n" ;
echo ' <tr>'."\n" ;
echo ' <td>'."\n" ;
echo ' <input type="hidden" name="'.$single_product->hide_name_b.'" value="'.$single_product->hide_value_b.'" />'.$single_product->short_desc.' $'.$adj_price.' USD'."\n" ;
echo ' </td>'."\n" ;
echo ' </tr>'."\n" ;
echo '</table>'."\n" ;
echo '<input type="hidden" name="currency_code" value="USD">'."\n" ;
echo '</form>'."\n" ;
/** This form will redraw the current page for approval. */
echo '<form action="ProductApprove.php" name="server_responder" method="post" target="_top">'."\n" ;
echo '<input type="hidden" name="trans" value="'.$transaction_start.'">'."\n" ;
echo '<input type="hidden" name="prod_id" value="'.$this->product_id.'">'."\n" ;
echo '</form>'."\n" ;
/** No form here just an input and a button. onClick will handle all the forms */
echo '<input type="image" src="https://www.sandbox.bank.com/en_US/i/btn/btn_purchaseimmediateCC_LG.gif" border="0" alt="This Bank - The safer, easier way to pay!" onclick="serverNotifySelected()">'."\n" ;
echo '<img alt="" border="0" src="https://www.sandbox.bank.com/en_US/i/scr/pixel.gif" width="1" height="1">'."\n" ;
This is the code for one button. The button will redraw the current page to go from purchasing to pre-approval AND also open a new window, give the new window focus and pass the new focused window to the payment provider.
This also prevents Chrome from blocking the new page from getting focus.
You can do this in deed.
There's an example how to, and even you can do it using cURL
<?php
//create array of data to be posted
$post_data['firstName'] = 'Name';
$post_data['action'] = 'Register';
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;
//we are going to need the length of the data string
$data_length = strlen($post_string);
//let's open the connection
$connection = fsockopen('www.domainname.com', 80);
//sending the data
fputs($connection, "POST /target_url.php HTTP/1.1\r\n");
fputs($connection, "Host: www.domainname.com \r\n");
fputs($connection,
"Content-Type: application/x-www-form-urlencoded\r\n");
fputs($connection, "Content-Length: $data_length\r\n");
fputs($connection, "Connection: close\r\n\r\n");
fputs($connection, $post_string);
//closing the connection
fclose($connection);
?>
Related
First, forgive me if this is an overly pedantic question. I have searched trying to find answers but perhaps I'm using the wrong search terms.
Trying to use an INI file for a simple PHP application, where there is an admin page to allow application options to be easily changed. I'm able to read in the ini file with no issue, problem I'm coming across is on the write - if any boolean values are false, they won't get put into the _POST and as such don't get written back into the ini file. Here's my sample:
settings.ini file:
[Site options]
bRequireLegal['Require NDA before badge print'] = true ;
bCollectVehicleInfo['Collect vehicle information'] = false;
bShowAdditionalMessageBeforeBadgePrint['Show badge printing message'] = true;
[Company info]
companyname['Company Name'] = 'The Company, Inc.' ;
Code to read in the ini file (settings.php):
$filepath = 'settings.ini'; //location of settings file
$settings = parse_ini_file($filepath, true, $scanner_mode = INI_SCANNER_TYPED);
//pull everything in ini file in as variable
foreach($settings as $section=>$options){
foreach($options as $option=>$values){
foreach($values as $descriptor=>$value){
if(is_bool($value) === true) {
${htmlspecialchars($option)} = +$value;
}
else ${htmlspecialchars($option)} = $value;
}
}
}
And finally, the options setting page:
<?php
include 'settings.php';
//after the form submit
if($_POST){
$data = $_POST;
update_ini_file($data, $filepath);
}
function update_ini_file($data, $filepath) {
$content = "";
//parse the ini file to get the sections
foreach($data as $section=>$options){
//append the section
$content .= "[".$section."]\r\n";
//append the values
foreach($options as $option=>$values){
$content .= $option;
foreach($values as $descriptor=>$value){
$content .= "['".$descriptor."'] = '".$value."';\r\n";
}
}
$content .= "\r\n";
}
if (!$handle = fopen($filepath, 'w')) {
return false;
}
$success = fwrite($handle, $content);
fclose($handle);
return $success;
}
?>
<html>
<body>
<?php
?>
<div class="container-fluid">
<form action="" method="post">
<?php
foreach($settings as $section=>$options){
echo "<h3>$section</h3>";
//keep the section as hidden text so we can update once the form submitted
echo "<input type='hidden' value='$section' name='$section' />";
//print all other values as input fields, so can edit.
foreach($options as $option=>$values){
foreach($values as $descriptor=>$value){
if(is_bool($value) === true) {
echo "<p>".$descriptor.": <input type='checkbox' name='{$section}[$option][$descriptor]' ".(($value===true)?" checked":"")." /></p>";
} else
echo "<p>".$descriptor.": <input type='text' name='{$section}[$option][$descriptor]' value='$value' />"."</p>";
}
}
echo "<br>";
}
?>
<input type="submit" value="Update INI" />
</form>
</div>
</body>
</html>
Any help would be greatly appreciated!
In your update_ini_file() function, replace this:
$content .= "['".$descriptor."'] = '".$value."';\r\n";
with
$content .= "['".$descriptor."'] = '".($value ? 'true' : 'false')."';\r\n";
This will cause it to write the strings 'true' and 'false' instead of literal Boolean values. See How to Convert Boolean to String
Edit to add:
I think you're generating your checkboxes incorrectly:
<input type='checkbox' name='{$section}[$option][$descriptor]' ".(($value===true)?" checked":"")." />
This will cause the 'true' boxes to be checked, but they will still lack a value (and thus will not be transmitted to the server when the form is submitted). You should change that code to:
<input type='checkbox' name='{$section}[$option][$descriptor]' value='1'".(($value===true)?" checked":"")." />
In other words, all checkboxes should have a value of '1', but the way the browser works, only those which are checked will be submitted.
Edit to add:
Checkboxes that are not checked will not get submitted. That explains why you are not seeing any output for values that are 'false': they simply don't get submitted. When you loop through $data (which comes from $_POST), it is missing those unchecked (and thus 'false') checkboxes.
Using a solution found here: POST unchecked HTML checkboxes
Change this:
echo "<p>".$descriptor.": <input type='checkbox' name='{$section}[$option][$descriptor]' ".(($value===true)?" checked":"")." /></p>";
to this, which includes a hidden field that has the value '0', which will get submitted even if the corresponding checkbox is unchecked:
echo "<p>".$descriptor.": <input type='hidden' name='{$section}[$option][$descriptor]' value='0'><input type='checkbox' name='{$section}[$option][$descriptor]' ".(($value===true)?" checked":"")." /></p>";
However, this has its own set of potential problems, specifically when a checkbox is checked, you will send two identically named fields: one with a '0' value and the other with a '1' value. This is explained in the link above, and is left as an exercise for you to solve (or ask for further details on) if my answer doesn't work.
I am fairly new to PHP and am having trouble with an assignment. The assignment is to create a simple address book in PHP, and i would like my address book to display all addresses that are in it along with a submission box at the bottom to add more addresses. Currently, I can get the addresses to display, but the submission box gives me an error ") Notice: Undefined variable: addres_add in C:\wamp64\www\address_tmp\address.php on line 18"
This is my code thus far, I snagged the submission box code from another answer here on StackOverflow, but I don't know how to modify it to fit my needs.
<?php
//Open address book file and print to user
$fh = fopen("address_book.txt", "r+");
echo file_get_contents("address_book.txt");
//Perfom submit function
if(isset($_POST['Submit']))
fseek($fh, 0, SEEK_END);
fwrite($fh, "$addres_add") or die("Could not write to file");
fclose($fh);
print("Address added successfully. Updated book:<br /><br />");
echo file_get_contents("address_book.txt");
{
$var = $_POST['any_name'];
}
?>
<?php
//HTML for submission box?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="any_name">
<input type="submit" name="submit">
</form>
<p>
You never assigned the variable from the form input. You need:
$addres_add = $_POST['any_name'];
fwrite($fh, "$addres_add") or die("Could not write to file");
Also, if you're just adding to the file, you should open it in "a" mode, not "r+". Then you don't need to seek to the end, that happens automatically.
You probably should put a newline between each record of the file, so it should be:
fwrite($fh, "$addres_add\n") or die("Could not write to file");
Otherwise, all the addresses will be on the same line.
Here is a simpler version of your program.
<?php
$file_path ="address_book.txt";
// Extract the file contents as a string
$file_contents = file_get_contents($file_path);
if ($file_contents) // Check if the file opened correctly
echo($file_contents . " \n"); // Echo contents (added newline for readability)
else
echo("Error opening file. \n");
// Make sure both form fields are set
if(isset($_POST['submit']) && isset($_POST['any_name']))
{
// Append the new name (used the newline character to make it more readable)
$file_contents .= $_POST["any_name"] ."\n";
// Write the new content string to the file
file_put_contents($file_path, $file_contents);
print("Address added successfully. Updated book:<br /><br />");
echo($file_contents);
}
else
{
echo("Both form elements must be set. \n");
}
?>
//HTML for submission box?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="any_name">
<input type="submit" name="submit">
</form>
Even with no comments it should be self explanatory. I leave the proper error dealing to you.
To answer your question, the error was being caused because the $address_add variable wasn't previously declared. You also added quotes to it, making it a string.
I'm have to build a "simple" application that will reads an xml file, prompts a user to choose a "response activity" in relation to what's on that file and send the whole thing (what was loaded + the activity chosen by the user) to a client over http in a new xml file.
So my questions is:
how can i add what's been loaded as well as the user chosen response activity onto a new xml file? and how do i send it to anyaddress.com?
i've also been told to use rest webservices, and although i have found a lot of information and examples online, nothing seem to be relevant to what i'm trying to do.
As you may have guessed, i'm new to all this. here' what i've done so far:
<?php
//reader.php
// Load the xml file and show what's on it
$xml = simplexml_load_file('xmlfile.xml')
or die("Could not open file!<hr /> ");
foreach($xml->children() as $child) {
foreach($child->children() as $young) {
echo $young->getName() . ": " . $young . "<br />";
}
}
// call the activity list according to what's on the xml file
if ($child->ACTIVITY == 'activity import') {
echo "<br/>" . file_get_contents('interface.php');
}
elseif($child->ACTIVITY == 'activity import special'){
echo "<br/>" . file_get_contents('interface1.php');
}
elseif($child->ACTIVITY == 'activity export'){
echo "<br/>" . file_get_contents('interface2.php');
}
else {
echo "<br/>" . 'incorrect activity';
}
// print the selected activity on page
if (isset($_POST['submit1'])) {
$selected_radio = $_POST['group1'];
print $selected_radio;
}
?>
this is one of the 3 forms i have for user input interface2.php
<form name="serv1" action="reader.php" method="POST">
<div align="left"><br>
<input type="radio" name="group1" value="OUTBOUND"> OUTBOUND<br/>
<input type="radio" name="group1" value="DONTPROCESS" checked> DON'T PROCESS<br/><br/>
<input type="submit" name="submit1" value="Return">
</div>
</form>
Any kind of help will be much appreciated.
You can perform edits on an XML file e.g. with SimpleXML or with DOMDocument.
You could post the result to another server with CURL.
I have a from that when submit runs a little php to export data to a csv. The php looks like :
$out = '';
if (isset($_POST['csv_hdr'])) {
$out .= $_POST['csv_hdr'];
$out .= "\n";
}
if (isset($_POST['csv_text'])) {
$out .= $_POST['csv_text'];
}
$filename = "z_".date("Y-n-d",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-n-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $out;
exit;
This works fine if I do a normal path to the php file like :
<form name="export" action="http://website.com/getcsv.php" method="post">
I am trying to move this php function into a controller now and call it that way. I am working on a magento admin module so I have to pass the url with the security key. So I move that function into an action in the controller :
public function getcsvAction(){
$out = '';
...
}
Then I am able to get the url with something like :
<?php echo Mage::helper("adminhtml")->getUrl("module/index/getcsv/");?>
This gives me a link with the key like :
http://website.com/module/index/getcsv/key/7431c859914c40d3f66dfcd1530813b3/
If I paste that link into the browser it executes the php fine. However when I replace it in my form action it no longer works and just does a redirect to the dashboard. I can not see any errors output and I am not sure what is happening. Any ideas on how to get this POST to work using a secure path as the action?
I found it thanks to this post.
I needed to add this to the form :
<input type="hidden" name="form_key" value="<? echo $this->getFormKey(); ?>" />
Your URL is missing the adminhtml area. Try this:
Mage::helper("adminhtml")->getUrl("*/module/index/getcsv/");
I am trying to pass array using form post method :
submit.php
<form method="post" action="makepub.php">
<?php
.... Loop
....
echo '</td><td align="center">';
echo '<input type="checkbox" name="file_list[]" value="'.$pr.'">' ;
echo '</td><tr/>';
....
.... Loop end
?>
makepub.php :
if (isset($_POST['submit1'])) {
$file_list = $_POST["file_list"];
$how_many = count($file_list);
echo '<b>Total No of Public files chosen : </b>'.$how_many.'<br><br>';
if ($how_many>0) {
echo '<b>You changed following files to public : </b><br>';
}
for ($i=0; $i<$how_many; $i++) {
echo ($i+1) . '- ' . $file_list[$i] . '<br>';
// Some code here
}
echo "<br><br>";
}
Ok these two files works perfectly on my localhost with XAMPP.
php version 5.3
but on my server array is not getting passed.
I checked by replacing the array with single variable. Even so nothing is passed to file makepub.php
Is there anything i am missing with post here ???
Any suggestion is appreciated.
Thanks.
Your code should work as it appears, however you should make sure your submit button has a name of submit1 and then you close the form with a closing tag.