Jump to content

Recommended Posts

Posted

I've written a form using JavScript math so I can input transaction details to generate receipts.

I've managed to use PHP to get the form to write to a CSV file which is linked to an access database so I can track all my transactions.

But I'm stuck.

I want to on submitting the form have the content of the form fields passed into a html template which I can print as a transaction receipt.

 

What do I need to add to my PHP file to have it generate a page containg the information from the form fields?

 

Any help is much appreciated.

 

Oh and I'm very new to all this stuff so go easy.

 

Thumbs Up

Posted

If I understand what you're asking, it should be pretty easy. In the php file that your form's "action" calls, output some html with content like:

 

Field Title: <?php echo $field; ?>

 

Format the output any way you want by normal html rules. This works for simple output from the form in real time.

 

If what you want is to create a file which you can then use later, you'd have to use the php Filesystem functions, create a new file, write the html into it and save it to your disk.

 

Is this what you're looking for, or have I missed it entirely?

Jim

Posted

I think so.

 

I merely want a new page to open containing data from the form which hasn't got all the buttons and stuff on.

 

I know virtually no PHP though I have just been editing stuff I can find .

 

This is what I've made so far

><?

$database_enabled = 1;
$database_file = 'form.csv';
$redirect_url = 'index.html';

// $database_fields = '*' all
// $database_fields = array('from', 'subject') array
$database_fields = '*'; 

/***************************************************************************/

function go_actions(){
   global $database_enabled;
   $form      = get_form_data();

   if ($database_enabled)
       save_form($form);
   redirect();
}

function redirect(){
   global $redirect_url;
   header("Location: $redirect_url");
   exit();
}


function save_form($vars){
   global $database_file, $database_fields;
   $f = fopen($database_file, 'a');
   if (!$f){
       die("Cannot open db file for save");
   }
   foreach ($vars as $k=>$v) {
       $vars[$k] = str_replace(array("|", "\r","\n"), array('_',' ',' '), $v);
   }
   if (is_array($database_fields)) {
       $vars_orig = $vars; 
       $vars = array();
       foreach ($database_fields as $k)
           $vars[$k] = $vars_orig[$k];
   }
   $str = join('|', $vars);
   fwrite($f, $str."\n");
   fclose($f);
}


function get_form_data(){
   global $REQUEST_METHOD;
   global $HTTP_POST_VARS;
   global $HTTP_GET_VARS;
   
   $vars = ($REQUEST_METHOD == 'GET') ? $HTTP_GET_VARS : $HTTP_POST_VARS;
   //strip spaces from all fields
   foreach ($vars as $k=>$v) $vars[$k] = trim($v);
   return $vars;
}



go_actions();
?>

 

All thats doing is saving the form fields to a csv and redirecting back to itself at the moment.

It could probably be done a lot simpler but I don't want to start hacking at the functions because it works?

 

Whats the verdict doc?

Posted

All you should have to do is not call the redirect. Instead, place normal html after the closing ?> at the bottom, using the 'echo' technique to display your field values. The simplest would be something like:

 

{html beginning stuff: head, body tags ... }

<?php

foreach ($vars as $k=>$v)

  echo "$k: $v<br>\n";

?>

Then maybe add a link back to the index page.

{html closing tags}

 

You're done.

 

Edit:

This is a quick and dirty piece of code. Of course you'll need to make your $vars array global and follow all the php rules and so on. But from the code you've supplied I can see you're already well up on that.

Posted

Thanks this is teaching me a lot,

 

Now I have:

?>

 

<html>

<head>

</head>

<body>

<?php

    global $REQUEST_METHOD;

    global $HTTP_POST_VARS;

    global $HTTP_GET_VARS;

 

    $vars = ($REQUEST_METHOD == 'GET') ? $HTTP_GET_VARS : $HTTP_POST_VARS;

    foreach ($vars as $k=>$v)

    echo "$k: $v<br>\n";

?>

</body>

</html>

Which works and produces a very crude [for now] page with all fields on.

 

How do I now exclude certain fields or include specific ones from the next page eg.hidden fields?

 

I'm guessing it's something to do with an array?

Posted

I don't know how many fields you are dealing with, put, regardless, this is how I, personally do it. For each field name, I set up a variable in the php script receiving the posted data.

 

$name = $_POST['name'];

$price = $_POST['price'];

 

and so on.

 

There are some forms for wich all of the fields can be wrapped up in an array. These are generally forms that I generate on the fly from a database and just assign array values to the field names, like

 

c[0]

c[1]

 

and so on.

 

Then, the script that receives the post data can be taken care of like

 

$c = $_POST['c'];

 

foreach($c as $d)

{

print("$d is the variable<br>");

}

 

 

This is ugly code... but it gets the point across.

 

What you are attempting to do is to send all of the post variables, some of them unwanted. I don't see of a simple way to strip out the data you don't want... but I'd love to see it done.

Posted

Forgive me if this is off base but why wouldnt you just make it all in PHP like;

 

<html>

 

<body>

 

<?php

 

if ($submit) {

 

// Here is where the processing starts

 

$db = mysql_connect("localhost", "bd_username", "db_password") or die("Could Not Connect To The Database. <br>" . mysql_error());

 

mysql_select_db("db_name",$db) or die("Could Not Select The Proper Database. <br>" . mysql_error());

 

$name = $_POST['name'];

$price = $_POST['price'];

 

 

 

$sql = "INSERT INTO db_name(name, desc, url) VALUES ('$name','$desc','$url')";

 

$result = mysql_query($sql);

 

 

if($result)

 

{

 

echo "I got yer data and I aint givin it back";

 

}

 

else

 

{

 

echo "We're sorry, the form you are trying to post to is no longer in service. Please check your data and try again";

 

}

 

 

 

echo $sql ;

 

echo "Thanks for the memories.\n";

 

}

 

else

 

{

 

 

 

// The form you see

 

 

 

?>

 

 

 

<form method="post" action="<?php echo $PHP_SELF?>">

 

Name:<input type="Text" name="name"><br>

 

Price:<input type="hidden" name="Price" value="<?php echo $price;?>"><br>

 

<input type="Submit" name="submit" value="Bring it on home.">

 

</form>

 

 

 

<?php

 

} // end if

 

?>

 

</body>

 

</html>

 

 

This would allow the "price" field to be hidden in this case. I am new to this so I may be off base but it looks like it would do the trick.

Posted

Heres the so far:

 

><?

$database_enabled = 1;
$database_file = 'form.csv';

// $database_fields = '*' all
// $database_fields = array('from', 'subject') array
$database_fields = '*';

$name = $_POST['requiredname']; 

/***************************************************************************/

function go_actions(){
   global $database_enabled;
   $form      = get_form_data();

   if ($database_enabled)
       save_form($form);
}

function save_form($vars){
   global $database_file, $database_fields;
   $f = fopen($database_file, 'a');
   if (!$f){
       die("Cannot open db file for save");
   }
   foreach ($vars as $k=>$v) {
       $vars[$k] = str_replace(array("|", "\r","\n"), array('_',' ',' '), $v);
   }
   if (is_array($database_fields)) {
       $vars_orig = $vars; 
       $vars = array();
       foreach ($database_fields as $k)
           $vars[$k] = $vars_orig[$k];
   }
   $str = join('|', $vars);
   fwrite($f, $str."\n");
   fclose($f);
}


function get_form_data(){
   global $REQUEST_METHOD;
   global $HTTP_POST_VARS;
   global $HTTP_GET_VARS;
   
   $vars = ($REQUEST_METHOD == 'GET') ? $HTTP_GET_VARS : $HTTP_POST_VARS;
   
   foreach ($vars as $k=>$v) $vars[$k] = trim($v);
   return $vars;
}



go_actions();
?>
<?php

$name = $_POST['requiredname'];
$date = $_POST['requireddate'];

$total = $_POST['requiredtotal'];
$exvat = $_POST['amount'];
$vat = $_POST['ex'];

$product1 = $_POST['requiredproduct1'];
$product2 = $_POST['product2'];
$product3 = $_POST['product3'];
$product4 = $_POST['product4'];
$product5 = $_POST['product5'];

$prod1price = $_POST['price1'];
$prod2price = $_POST['price2'];
$prod3price = $_POST['price3'];
$prod4price = $_POST['price4'];
$prod5price = $_POST['price5'];

$qty1 = $_POST['qty1'];
$qty2 = $_POST['qty2'];
$qty3 = $_POST['qty3'];
$qty4 = $_POST['qty4'];
$qty5 = $_POST['qty5'];

$address1 = $_POST['address1'];
$address2 = $_POST['address2'];

$warranty1 = $_POST['warranty1'];
$warranty2 = $_POST['warranty2'];
$warranty3 = $_POST['warranty3'];
$warranty4 = $_POST['warranty4'];
$warranty5 = $_POST['warranty5'];

echo '
<html>
<head>
<script LANGUAGE="JavaScript">
<!-- Begin 
function goBack(){
var targetURL="index.html"
window.print()
if (confirm("Go back?")){
window.location=targetURL
}
else{
}
}
//  End -->
</script>
</head><body onload="goBack()">
';
PRINT "<FONT SIZE=-1 face=verdana>";. . .
< . . . . .all the page stuff >
PRINT "</body>";
PRINT "</html>";

?>

 

Now what i want to do is have the form only save certain fields to the csv file.

Do I need to respecify all the variables again and which part do I have to change to make the form save only the fields want?

 

This is actually the firsty time I've ever done any PHP, the first part of the script was just an assembly of bits of other script and tutorials.

 

At the moment it is saving all the fields to a csv and opening a page for printing.

I'm not sure if there is stuff in there I don't need but it works so I'm sticking with it for now.

 

Thanks

:)

Posted

First, I totally agree with Critical Mass. If I were writing this code, I'd do it all server side. PHP would be my weapon of choice. But I'm biased.

 

Here's what is going on in the script you have written so far.

 

You have several Java functions.

 

The first function calls the other two.

 

Your second function does the writing to the file. You have chosen to iterate through an array of variables that is created by your third function. This is important, because you will need to choose to either create your array more proactively and leave some variables out of the array, or you'll need to ditch the array concept altogether and rewrite the second function to write the variables one by one.

 

The third function creates your array. It isn't doing a proactive, selective job... it's grabbing everything. Including some stuff you apparently don't want.

 

On top of that, the third function is redundant given that you are compiling your post data selectively with PHP. So basically, the php stuff you've added works, your javascript just isn't using the data.

 

The big question is, do you want to change the php, or the javascript? Will the javascript be rewritten to accomodate the php variables, or will you rewrite the php to create a better array for the javascript.

 

From looking at it, I would guess that the easiest thing to do would be to

1- eliminate out the third function

2- modify the php like so

 

$vars[0]= $_POST['requiredname'];

$vars[1]= $_POST['requireddate'];

 

$vars[2] = $_POST['requiredtotal'];

$vars[3]= $_POST['amount'];

$vars[4]= $_POST['ex'];

 

$vars[5]= $_POST['requiredproduct1'];

 

and so on...

 

You are using some javascript that doesn't look familiar to me... so I can't be 100% that the code I wrote will work as-is. But you will find that my dissection of your script to be dead-on.

 

Another reason why I would write this with server side code... will this script work if the user has javascript turned off? I don't think it will. Having said this, I'm referring to maybe 1% or 2% of the world.

Posted

The script is only ever being used offline by me so user compatability isn't important.

 

I think I've ended up with an overly complicated script doing a very simple job.

 

All I want is the script to save certain fields to a csv and display cetain other fields in a new page.

 

I have a form that is used to enter transaction and customers details, on submitting the form I want to display a reciept containing some of the values from the form formatted for printing.

The bit at the bottom.

This is working nicely.

I also want the script to save certain fields to a csv file so I can find transactions later if necessary.

 

I would rather not have the javascript at the top at all.

All I want the first part to do is save specific fields to a csv file.

I think it is designed to do other things as well which are unecessary, hence why it uses complicated language.

 

Can the saving to a cvs file be done a lot simpler than that and with specific fields?

Say with simple PHP?

Posted
Can the saving to a cvs file be done a lot simpler than that and with specific fields?

Say with simple PHP?

 

Yes.

 

I would suggest that you look in www.hotscripts.com if you haven't already just to make sure that someone else hasn't created this script or something very similar.

 

From other posts that I have read from you, I know you are quite skilled at javascript but are still learning php. PHP is easy to learn, but there is a learning curve.

 

If you don't have a book on php, then I would check out fopen and fwrite at www.php.net

 

It seems like you really want to save it to a cvs... but I'd stongly recommend a database if cvs isn't critical.

Posted

I'll get that book when I get paid thanks.

 

A data base would be ideal but it would probably have to be an access database as the script is running offline.

 

I just assumed it would be way more complicated though so I tried a csv thinking it would be simpler.

 

I have looked at hundreds on hotscript site [CoOol site].

The only problem is they all do lots of unecessary things aswell not least sending out emails which I don't want, and as you guessed I am novice at php and did not want to try editing one as I wouldn't be sure what to take out.

 

I might be brave though and have a go making back ups as I go, that way I'll probably learn something.

 

Thanks for the advice surefire.

 

Thumbs Up

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...