How transmit & receive String from a php script

Share your advanced knowledge/code with the community.
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

How transmit & receive String from a php script

Post by falsam »

ShowCase and download : http://s242132022.onlinehome.fr/sb-demophp1

:arrow: Code SpiderBasic

Code: Select all

Enumeration
  #mainform
  #fmuserslist
  #fmuser
  #fmlocation
  #fmuserupdate
  #fmuserslistrefresh
  #fmdownload
EndEnumeration

Procedure.i WindowElement(Window, UseJquery.b=#True)
  Protected winObject=WindowID(Window) 
  !return (v_winObject && v_winObject.element)? v_UseJquery? $(v_winObject.element):v_winObject.element:null; 
EndProcedure

Procedure.i GadgetElement(Gadget, UseJquery.b=#True)
  Protected gadgetObject=GadgetID(Gadget) 
  !return (v_gadgetObject && v_gadgetObject.div)? v_UseJquery? $(v_gadgetObject.div):v_gadgetObject.div:null; 
EndProcedure

;CallBack : Update listicongadget #fmuserslist with all users
Procedure CallBack_UsersList(ReceivedData.s)
  Protected buffer.s
  Protected n.i
  Protected p.i
  
  Protected user.s
  Protected location.s
  
  ClearGadgetItems(#fmuserslist)
  
  For n = 1 To CountString(ReceivedData, #CRLF$)
    buffer = StringField(Trim(ReceivedData), n, #CRLF$)
        
    For p = 1 To CountString(buffer, "|") + 1
      Select p
        Case 1
          user = StringField(buffer, 1, "|")
        Case 2
          location = StringField(buffer, 2, "|")
      EndSelect
    Next
    
    AddGadgetItem(#fmuserslist, -1, user + Chr(10) + location)
  Next
  SetGadgetText(#fmuserslistrefresh, "Users list refresh")
EndProcedure

;Get users list : Run core.php - Call GetData_UsersList() - Call GetData_Error()
Procedure UsersList()
  Protected buffer.s = ""
  
  SetGadgetText(#fmuserslistrefresh, "Wait ....")
  
  !$.ajax({
  !   type: "GET",
  !   url: 'core.php',
  !   data: { action: "userslist" },
    
  !   success : function(data) {
  !       v_buffer = data
          CallBack_UsersList(buffer);
  !    },
    
  !   error : function() {
          MessageRequester("Error", "File users not exist")
  !    }
    
  !});
EndProcedure

;CallBack : Update listicongadget #fmuserslist with the new user
Procedure CallBack_UserAdd(user.s, location.s)
  Protected element = GadgetElement(#fmuserslist, #False)
  
  AddGadgetItem(#fmuserslist, -1, user + Chr(10) + location)
  SetGadgetState(#fmuserslist, CountGadgetItems(#fmuserslist)-1)
  
  SetGadgetText(#fmuser, "")
  SetGadgetText(#fmlocation, "")  
  
  ;Fonctionne pas 
  !$(v_element).scrollTop(500);
EndProcedure

;Add user
Procedure UserAdd()  
  Protected user.s = Trim(GetGadgetText(#fmuser))
  Protected location.s = Trim(GetGadgetText(#fmlocation))
  
  If user = ""
    MessageRequester("Information", "The username you entered is too short.") 
  Else
    user = RemoveString(user, "|")
    location = RemoveString(location, "|") 
    
    !$.ajax({
    !   type: "GET",
    !   url: 'core.php',
    !   data: { action: "update", user: v_user, location: v_location },
      
    !   success : function(data) {
           CallBack_UserAdd(user, location);
    !    },
      
    !   error : function() {
           MessageRequester("Error", "File users not exist")
    !    }
      
    !});    
  EndIf
    
EndProcedure

;Show MainForm
Procedure WindowShow()
  OpenWindow(#mainform, 0, 0, 0, 0, "", #PB_Window_Background)
  
  TextGadget(#PB_Any, 50, 30, 800, 22, "<h2><strong>Spider Basic</strong> Demo : write data to file</h2>")
  
  ListIconGadget(#fmuserslist, 50, 80, 500, 500, "User", 300)
  AddGadgetColumn(#fmuserslist, 1, "Location", 200)
  
  FrameGadget(#PB_Any, 560, 72, 300, 510, "Contact")
  
  TextGadget(#PB_Any, 580, 112, 50, 25, "User")
  StringGadget(#fmuser, 640, 110, 200, 25, "")
  
  TextGadget(#PB_Any, 580, 157, 50, 25, "Location")
  StringGadget(#fmlocation, 640, 155, 200, 25, "")
  
  ButtonGadget(#fmuserupdate, 640, 200, 80, 22, "Update")
  GadgetToolTip(#fmuserupdate, "Record data")
  
  TextGadget(#fmdownload, 580, 550, 100, 22, "<a href="+Chr(34)+"http://s242132022.onlinehome.fr/sb-demophp1/demophp1.zip"+Chr(34)+">Download Source</a>")
   
  TextGadget(#PB_Any, 50, 600, 280, 22, "Powered by <b>SpiderBasic</b>") 
  
  ButtonGadget(#fmuserslistrefresh, 440, 600, 100, 22, "Users list refresh")
         
  BindGadgetEvent(#fmuserupdate, @UserAdd())
  BindGadgetEvent(#fmuserslistrefresh, @UsersList())
EndProcedure

;-Start
WindowShow()
UsersList()
:arrow: Code PHP

Code: Select all

<?php
if(isset($_GET['action']))
{
	$file = "users.txt";
	
	$crlf= "\r\n";
	
	if($_GET['action']=='update' AND isset($_GET['user']) AND isset($_GET['location']))
	{
	file_put_contents($file, $_GET['user']."|".$_GET['location'].$crlf, FILE_APPEND | LOCK_EX);
	echo "Update";
	}
	elseif($_GET['action']=='userslist')
	{
	$buffer = file_get_contents($file);
	echo $buffer;
	}
}
?>
I do not know if this is the right approach, but it works. You know what? This code is not very funny :lol:

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
User avatar
useful
Posts: 116
Joined: Tue Feb 25, 2014 1:15 pm

Re: How transmit & receive String from a php script

Post by useful »

I haven't looked into the details, (waiting for the actual documentation from Fred), but in this example, the question: is it possible the same thing using HTTPRequest()?
2B or not 2B = FF
falsam
Posts: 280
Joined: Mon May 05, 2014 9:49 pm
Location: France
Contact:

Re: How transmit & receive String from a php script

Post by falsam »

gosh! I have not seen the implementation of HTTPRequest (). I take a look at this function. I think HTTPRequest () uses Ajax.

➽ Windows 11 - JDK 1.8 - SB 2.40 - Android 13
http://falsam.com

Sorry for my poor english
Post Reply