Recent

Author Topic: Free Spider and array  (Read 5404 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Free Spider and array
« on: May 29, 2015, 03:12:09 pm »
hello guys, I have to create a cgi with free spider and Lazarus. I need to figure out how to move from html / javascript array that my cgi must then prepare. for example with the following code to create an array of three elements and try to send it to my cgi. How do I read this data from a cgi free spider? thanks

Code: [Select]
<html>
<head>
<title>Test tabelloni</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script type="text/javascript" src="test.js"></script>

<script type="text/javascript">

function ElaboraTabellone()
{
var Nomi=["hi","caio","sempronio"];

$.ajax({
url: 'cgi-bin/Tabelloni',
type: 'POST',
dataType: "html",
data: {
numero_atleti: 3,
arrayNomi: NomiDaInviare
},
success: function (response)
{
$('#mytable').html(response);
},
error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + errortext); },
async: true
});
}
 
$(document).ready(function() {
alert('ok');
ElaboraTabellone();
    });
</script>

</head>
<body>

<div id='mytable'></div>

</body>
</html>
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: Free Spider and array
« Reply #1 on: May 29, 2015, 05:42:52 pm »
I have no experience on Ajax, but I have experience with FreeSpider, so that I could help in two cases:

1. If you write a sample of server side in other language, such as PHP or Java, I could give you FreeSpider version of it
2. If you remove Ajax temporary and replace it with normal form fields

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Free Spider and array
« Reply #2 on: May 29, 2015, 08:50:49 pm »

The problem is that I need to understand how to read the array passed as POST on free pascal.
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Free Spider and array
« Reply #3 on: May 29, 2015, 08:53:15 pm »
I have no experience on Ajax, but I have experience with FreeSpider, so that I could help in two cases:

1. If you write a sample of server side in other language, such as PHP or Java, I could give you FreeSpider version of it
2. If you remove Ajax temporary and replace it with normal form fields

But then the php you know him?

You can not do a simple example to switch from a free php pascal array. Any one, an array of strings for example. I have to figure out how to do it. thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

nomorelogic

  • Full Member
  • ***
  • Posts: 165
Re: Free Spider and array
« Reply #4 on: May 29, 2015, 10:39:42 pm »
The problem is that I need to understand how to read the array passed as POST on free pascal.

I never used freespider, btw, a (post) web request should be readable using a method called like, e.g., "QueryFields".

Can you post the web request here?

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: Free Spider and array
« Reply #5 on: May 29, 2015, 10:44:46 pm »
you can use Request.ContentFields to read posted data

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: Free Spider and array
« Reply #6 on: May 30, 2015, 06:56:59 am »
You can pass array in one field such as using textarea, or by using text fields/hidden fields with the same name. Then you can receive them using request.ContentFields property.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: Free Spider and array
« Reply #7 on: June 01, 2015, 05:46:26 pm »
Ok, I made progress. My javascript code is:
Code: [Select]
function ElaboraTabellone()
{
//var Nomi=["hi","caio","sempronio"];
var info = [];
info[0] = 'hi';
info[1] = 'hello';
info[2] = 'pippo';
$.ajax({
url: 'cgi-bin/Tabelloni',
type: 'POST',
dataType: "html",
//dataType: "json",
data: {
numero_atleti: 3,
arrayNomi: JSON.stringify({ lunghezza: info.length, valori: info })
},
success: function (response)
{
$('#mytable').html(response);
},
error: function(XMLHttpRequest, textStatus, exception) { alert("Ajax failure\n" + errortext); },
async: true
});
}
Lazarus is my code:
Code: [Select]
procedure TDataModule1.SpiderCGI1Request(Sender: TObject;
  Request: TSpiderRequest; var Response: TSpiderResponse);
var
  Num_Atleti: string;
  i: integer;
begin
     //Qui inizia il codice del mio programma

     //leggo i miei parametri
     Num_Atleti:=Request.Form('numero_atleti');
     Response.Add('Num atleti: ' + Num_Atleti + '<br>');
     Response.Add('1. ' + Request.ContentFields.values['arrayNomi[]']);
     Response.Add('2. ' + Request.ContentFields.values['arrayNomi[]']);
     for i:=0 to Request.ContentFields.Count-1 do
     begin
          Response.Add(Request.ContentFields.Strings[i]);
     end;
     Response.Add('Fine' + '<br>');
end;
My result on browser


Num atleti: 3
1. 2. numero_atleti=3 arrayNomi={"lunghezza":3,"valori":["hi","hello","pippo"]} Fine

Now I want to understand how I can read data

valori":["hi","hello","pippo"]

who tells me how to do?

Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

motaz

  • Sr. Member
  • ****
  • Posts: 495
    • http://code.sd
Re: Free Spider and array
« Reply #8 on: June 02, 2015, 12:05:40 pm »
This is an example of how to read data from array in FreeSpider:

Code: [Select]
procedure TDataModule1.SpiderCGI1Request(Sender: TObject;
  Request: TSpiderRequest; var Response: TSpiderResponse);
var
  i: Integer;
begin
  response.Add('You have sent: <br/>');
  for i:= 0 to request.ContentFields.Count - 1 do
    response.Add(request.ContentFields.Names[i]  + ':' + Request.ContentFields.ValueFromIndex[i] + '<br/>');
  response.Add('<hr>');
  response.Add('<form method=post>');
  response.Add('<input type=text name=mydata /><br/>');
  response.Add('<input type=text name=mydata /><br/>');
  response.Add('<input type=text name=mydata /><br/>');
  response.Add('<input type=text name=mydata /><br/>');
  response.Add('<input type=text name=mydata /><br/>');
  response.Add('<input type=submit />');
  response.Add('</form>');
end;

 

TinyPortal © 2005-2018