Jump to content

Extracting data from a form into a text file


IanArcher

Recommended Posts

i want to place the contents of values from a form into a text file. When an option is changed in the form and submit button is clicked, the user is taken to another page where info about the company's address, website and so are displayed line after line like so: Member Search Results: Company800-555-555Address Line 1Address Line 2Website: http://example.com Though the text file shows up blank. How do i go about achieving my desired result?My code i tried originally:

<html><form action="http://*****.com/business_detail_framed.asp" method="post" name="b1"><Select Name="select_business" onChange="b1.input1.value=b1.select_business[this.selectedIndex].text"><option value="5102" name="float">Company One</option><option value="5053" name="float">Company Two</option><option value="5091" name="float">Company 3</option></select><input value="Submit" type="submit" onchange="this.form.submit()">;</form> <?php file_put_contents("businessinfo.txt", $_POST['float'], FILE_APPEND); $g = file_get_contents("businessinfo.txt");echo $g; $r = $_REQUEST['float'];$r .= $_POST['float']; echo $r; ?> 

And the code i tried with a little help, but still no luck, the text file still shows up empty:

if (isset($_REQUEST['Submit'])) {  file_put_contents("businessinfo.txt", $_REQUEST['select_business'] . "\n", FILE_APPEND);

Any suggestions?

Link to comment
Share on other sites

Yea, PHP has permission to create/write the file, permissions are set at 644 and also i did

var_dump($_POST['select_business'])

and it returns NULLIT should be noted that the form action is linked to a .asp file that sets the connection to a database that holds these records and displays them in a certain format. I am able to see these records by clicking the submit button, but i need to extract the data to a text file. I just included 3 options in the form as a test for the rest of the process. The contents of the .asp file is here if you need to see it. pastebin.com/BSUqBX8k

Link to comment
Share on other sites

but did you check the return value of file_put_contents? I don't see you testing that all, or confirming if you have based on your last post. I also don't get why you aren't just submitting the form. Why all the JS when the form will just submit the value of the selected dropdown?

Link to comment
Share on other sites

but did you check the return value of file_put_contents? I don't see you testing that all, or confirming if you have based on your last post. I also don't get why you aren't just submitting the form. Why all the JS when the form will just submit the value of the selected dropdown?
Not really sure what your getting at? How do i/How do you mean check the return value?
Link to comment
Share on other sites

Where are you posting the named input value for 'Submit' ? that you are checking against, I see 'b1', 'select_business', 'float' (which is invalid as the name value of select is used), but no 'Submit', its not getting passif (isset($_REQUEST['Submit'])) {to write to text file, because it does exist in the first place when the form is submitted <input value="Submit" name="Submit" type="submit" onchange="this.form.submit()">;

Link to comment
Share on other sites

Not really sure what your getting at? How do i/How do you mean check the return value?
do you know how functions work? they (always?) return something, and in this case, as described in the link, it can return true or false, based on success or failure. i.e.
if(file_put_contents('business_info.txt', $_POST['float'], FILE_APPEND)){  echo 'success';}else{  echo 'failure';};

Link to comment
Share on other sites

I'm using the below code, combination of both of the previous posts, still no luck. I received advice form another forum that maybe i should use asp functions to write the records retrieved to a file, but i have no experience in asp, though i did research the method as well. Anyone with asp experience enough to write me an example of that?

<html><form action="http://******.com/business_detail_framed.asp" method="post" name="b1"><select name="select_business" ><option value="5102" name="float">1 Stop Signs</option><option value="5053" name="float">23/6 Services, LLC</option><option value="5091" name="float">A Touch Above</option></select><input value="Submit" name="Submit" type="submit"></form><?phpif (isset($_REQUEST['Submit'])) {  if(file_put_contents("businessinfo.txt", $_POST['float'] . "\n", FILE_APPEND)) {  echo 'success';  } else  {  echo 'failure';  }}?></html>

Link to comment
Share on other sites

As mentioned before, using the name 'float' assigned to each option is invalid, the value selected is passed through 'select_business'
if(file_put_contents("businessinfo.txt", $_POST['select_business'] . "\n", FILE_APPEND))

I tried that as well, but nothing is still being written to the file.
Link to comment
Share on other sites

With your example you have the form and php on the same page, but you are sending it to "http://******.com/business_detail_framed.asp", for this to work you would have to send form post values to itself so the php code can process the info, and it must be a php server/page for this processing to work. formpage.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><form action="formpage.php" method="post"><select name="select_business" ><option value="5102">Company One</option><option value="5053">Company Two</option><option value="5091">Company 3</option></select><input value="Submit" id="Submit" name="Submit" type="submit" />;</form><?php if (isset($_REQUEST['Submit'])) {  if(file_put_contents("businessinfo.txt", $_POST['select_business'] . "\n", FILE_APPEND)){  echo 'success';  } else  {  echo 'failure';  }}?></body></html>

Or send it to another page to process formpage2.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><form action="responsepage.php" method="post"><select name="select_business" ><option value="5102">Company One</option><option value="5053">Company Two</option><option value="5091">Company 3</option></select><input value="Submit" id="Submit" name="Submit" type="submit" />;</form></body></html>

responsepage.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><?phpif (isset($_REQUEST['Submit'])) {  if(file_put_contents("businessinfo.txt", $_POST['select_business'] . "\n", FILE_APPEND)){  echo 'success';  } else  {  echo 'failure';  }}?></body></html>

Link to comment
Share on other sites

That's just it, the form action depends solely on linking to this .asp file which obtains the records from the database. Now i could've easily obtained these records using a mysql_result command had i possessed the login details to whatever mysql database this info was stored on, because i had checked and the records aren't stored on the servers database. and the login credentials are considered lost to where it is located. So when i changed the form action the records were no longer accessible.I had even included an INTO OUTFILE function in the SQL statement to no avail.

Link to comment
Share on other sites

Why don't you try running the php code through ajax request on submisstion, gather the values on submission and send it though $_GET to php file.
I wouldn't know where to start on that, could you write me an example?
Link to comment
Share on other sites

Did you check for the database credentials in the ASP code? Or, and call me crazy here, but maybe it would be a better idea to change the password so that you actually have control over the database server. Do you really have a database server just running out there that no one is able to change or fix if it breaks?

Link to comment
Share on other sites

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/var xmlhttp, thisform;function ajax_update(frm, e){thisform=frm;e.preventDefault ? e.preventDefault() : e.returnValue = false;x=document.getElementById("select_business").valuestr="select_business="+x;//alert(str)xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Your browser does not support AJAX!");  return;  }var url="formresponse.php?"+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);}function stateChanged(){if (xmlhttp.readyState==4){if(xmlhttp.responseText=="success"){thisform.submit();}}}function GetXmlHttpObject(){if (window.XMLHttpRequest)  {  // code for IE7+, Firefox, Chrome, Opera, Safari  return new XMLHttpRequest();  }if (window.ActiveXObject)  {  // code for IE6, IE5  return new ActiveXObject("Microsoft.XMLHTTP");  }return null;}/*--*//*]]>*/</script> </head><body><form action="http://******.com/business_detail_framed.asp" method="post"  onsubmit="ajax_update(this, event)"><select name="select_business" id="select_business"><option value="5102">Company One</option><option value="5053">Company Two</option><option value="5091">Company 3</option></select><input value="Submit" id="Submit" name="Submit" type="submit" />; </form></body></html>

formresponse.php

<?php  if(file_put_contents("businessinfo.txt", ($_GET['select_business']."\n"), FILE_APPEND))  {  echo "success";  }  else  {	echo "failure";   }?>

Link to comment
Share on other sites

Did you check for the database credentials in the ASP code? Or, and call me crazy here, but maybe it would be a better idea to change the password so that you actually have control over the database server. Do you really have a database server just running out there that no one is able to change or fix if it breaks?
I know exactly what you mean.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title><script type="text/javascript">/*<![CDATA[*//*---->*/var xmlhttp, thisform;function ajax_update(frm, e){thisform=frm;e.preventDefault ? e.preventDefault() : e.returnValue = false;x=document.getElementById("select_business").valuestr="select_business="+x;//alert(str)xmlhttp=GetXmlHttpObject();if (xmlhttp==null)  {  alert ("Your browser does not support AJAX!");  return;  }var url="formresponse.php?"+str;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);}function stateChanged(){if (xmlhttp.readyState==4){if(xmlhttp.responseText=="success"){thisform.submit();}}}function GetXmlHttpObject(){if (window.XMLHttpRequest)  {  // code for IE7+, Firefox, Chrome, Opera, Safari  return new XMLHttpRequest();  }if (window.ActiveXObject)  {  // code for IE6, IE5  return new ActiveXObject("Microsoft.XMLHTTP");  }return null;}/*--*//*]]>*/</script> </head><body><form action="http://******.com/business_detail_framed.asp" method="post"  onsubmit="ajax_update(this, event)"><select name="select_business" id="select_business"><option value="5102">Company One</option><option value="5053">Company Two</option><option value="5091">Company 3</option></select><input value="Submit" id="Submit" name="Submit" type="submit" />; </form></body></html>

formresponse.php

<?php  if(file_put_contents("businessinfo.txt", ($_GET['select_business']."\n"), FILE_APPEND))  {  echo "success";  }  else  {	echo "failure";   }?>

This works, though it only dumps the value of the selected option into the text file, i.e. (5102)I modified it with a getElementsByName as well and changed a few things to see if that would correct it, but no, so i reverted it back.
Link to comment
Share on other sites

you will have to loop through all the inputs in the form you wish added to the text file and add them to the querystring sent to the php file str="select_business="+x;so you end up with str="select_business="+x+"&querystringname2="+qsvalue2+"&querystringname3="+qsvalue3; php file reading these asif(file_put_contents("businessinfo.txt", ($_GET['select_business'].$_GET['querystringname2'].$_GET['querystringname3']."\n"), FILE_APPEND))

Link to comment
Share on other sites

you will have to loop through all the inputs in the form you wish added to the text file and add them to the querystring sent to the php file str="select_business="+x; so you end up with str="select_business="+x+"&querystringname2="+qsvalue2+"&querystringname3="+qsvalue3;php file reading these as if(file_put_contents("businessinfo.txt", ($_GET['select_business'].$_GET['querystringname2'].$_GET['querystringname3']."\n"), FILE_APPEND))
I modified it as you said, though now the form isn't responding at all. What i mean by that is, when the submit button is clicked i am no longer redirected to the page. I reverted back, but have a look
Link to comment
Share on other sites

are you doing any debugging on your own? adding logging/alerts/echos to your script? looking in the console for errors? what does the broken code look like though?

Link to comment
Share on other sites

are you doing any debugging on your own? adding logging/alerts/echos to your script? looking in the console for errors? what does the broken code look like though?
I have indeed checked the error log for messages, though none are present there that pertain to the current script. The code dsonesuk provided is very close to extracting the records but it simply extracts the value number. I changed the first option in the form to id="Stop" and the getElementById() variable to "Stop" as well, and it only extracted the value number to the text file as well, i am trying things on my own not asking for spoonfeeding you see?
Link to comment
Share on other sites

This works, though it only dumps the value of the selected option into the text file, i.e. (5102)

?? Let me get this straight! ARE you saying you require ALL the values from dropdown? ie option values 5102, 5053, 5091 and not just the selected option to go to the text file.

Link to comment
Share on other sites

This works, though it only dumps the value of the selected option into the text file, i.e. (5102)

?? Let me get this straight! ARE you saying you require ALL the values from dropdown? ie option values 5102, 5053, 5091 and not just the selected option to go to the text file.

Yeah that's what i wanted to do, but i just want ot focusing on getting the business info for those values extracted, and once i handle that i could just loop through each option one-by-one to append to the text file the business info for each selected option.
Link to comment
Share on other sites

?? if you want the value of options use

x=document.getElementById("select_business")y=new Array();for(i=0;i<x.options.length;i++){y[i]=x.options[i].value}str="select_business="+y;

else text of option list use

x=document.getElementById("select_business")y=new Array();for(i=0;i<x.options.length;i++){y[i]=x.options[i].text}str="select_business="+y;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...