Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

HTML form and PHP V1

 
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi,

This thread is and updated version of previous thread HTML form and PHP which was closed by some experts without interpreting correc conclusion of all the discussions in previous threads.

So in continuation with previous thread I had same query like below :-

Is it possible to create an web-page,form,that would accept specific input and generate
an XML file from it.The form has to have the ability to add items in one particular section(where we can
add additional streams) and give us the ability to remove streams as well.
For each stream we must be able to attach a logo for that stream as well.

Xojo or Php for this could be used,

below is the sample XML file and Data Format.




What I have done so far 1) Created HTML form like below :-




So After designing above form I am stuck how to code once we click submit button. So request experts step by step process and code exmple to proceed further with respect to current problem.


Thanks much..
 
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
So what PHP have you written, and what are you stuck on?
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
below is the code which i have tried but now the different error is coming when i click on submit button after uploading some file from my desktop i get an error like'file not found'
whereas file does exist on desktop itself.

 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Obviously nobody can help you if you don't post the exact error message, tell us where the error message appears, and what URL the browser is directed to.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
please find attached error message.
error.png
[Thumbnail for error.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
As you have been told before, you need to use the proper relative web path as your form action, NOT a file path.
 
Saloon Keeper
Posts: 21981
150
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
In answer to your question: Yes.

As to details, that's pretty basic PHP. Go find a good book.

But PHP Version 1? The current release is somewhere around 7.3, I think. Nevertheless, even PHP version 1 could do what you want.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
what do you mean by " proper relative web path as your form action, NOT a file path."

i have my html file in my v1 folder which again is inside htdocs folder.
previously it was able to select the file and gave some different errors but it never gave errors like that 'file not found'  this time it is giving this error.

..and just to clarify from version i just meant that heading of this topic is slightly changed from 'HTML form and PHP' to 'HTML form and PHP V1' so that we could discuss this time with new enthusiasm to solve this old problem and I did not mean from any PHP software related version here
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
I meant you are using the wrong value for the action attribute of your HTML form. I thought that you are using a file path. We can't tell however, because you never showed us the updated version of your HTML form.

However, it's more likely that you DID use a relative path (which is good) but you opened the HTML page containing your form using the file:// protocol, which we told you before you must not do. You need to open your HTML page using the http:// protocol.
 
Tim Holloway
Saloon Keeper
Posts: 21981
150
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator

John Ken wrote:what do you mean by " proper relative web path as your form action, NOT a file path."



URLs are not filesystem paths, even though they have similar syntax to Unix filesystem paths. Nevertheless, a URL is a resource locator, not a file locator.

Confusion arises since in many web application servers, if you provide an extended URL path - that is, one that includes not only the essential server/port/protocol and application context path (which may simply be "/"), the rest of the path may be interpreted by the application server as a filesystem location relative to some fixed point defined by that application server. So http://localhost/mywebapp/images/catpic.jpg might be translated by the application server as a request to open a file located at /var/www/html/images/catpic.jpg and copy its contents to the HTTP response output stream. Because a web server is not a file server. But that entirely depends on how the appserver has been configured. In Java, I could just as easily configure the webapp context path /images/catpic.jpg to invoke a servlet program and not a file at all.
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
I bet you openen your HTML page by double clicking on the .html file in your /htdocs/v1 folder.

Don't do this. If you can't be bothered to type the HTTP URI for your web page into your browser's address bar, then make a bookmark in your browser.
 
Tim Holloway
Saloon Keeper
Posts: 21981
150
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Report post to moderator
Also, just to be totally clear, a webapp server cannot open files on the web client. It can only access files local to the server machine and its LAN mountpoints. When you do a file upload, you're actually requesting the client to open and copy a client file's contents to the HTTP request stream and when a webapp server does a file download, it includes a request to the client to save data embedded in the responsed data stream. The client may reject this response and not store the data at its option. A web browser client program will typically present a "save as..." dialog with perhaps a recommended save name/location, but in this case, the user can chose where - or if - to save that data in a file of their choosing.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
after typing just localhost/v1/ a web page opened displaying contents of v1 folder and from there when i clicked form1.html and after selecting some file and then clicked submit button then got attached error now.

please see attachments or the same reference.

index-of-v1.png
[Thumbnail for index-of-v1.png]
object-not-found-error-404.png
[Thumbnail for object-not-found-error-404.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
You really should be able to solve this kind of problem yourself. Apache is almost literally telling you that you are using the wrong URL. Do you not see anything out of the ordinary when you compare the URL with the name of your PHP file?
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
but why is it taking additem.php whereas i have aditem.php in v1 folder?
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Because that's where you told your HTML form to submit its data to.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
no,i never told my HTML form to submit it's data to,how could you say so?
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Okay then. Show us form1.html.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
it's saved as a chrome icon so not sure how could i go inside it's code but can show all the objects in side v1 folder as attached here.
form1.png
[Thumbnail for form1.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Just drag it into your text editor.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok,corrected additem to aditem after editing it in my pycharm editor which comes as a default editor on my system if i right click on that form1.html file. but despite correcting the same error is appearing please refer attached inside code of form1.html file.
code-corrected.png
[Thumbnail for code-corrected.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator

Stephan van Hulst wrote:Obviously nobody can help you if you don't post the exact error message, tell us where the error message appears, and what URL the browser is directed to.

 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
same error screen shot is attached here in screen shot whereas code is corrected also attaching corrected code screen shot as well.
same-error.png
[Thumbnail for same-error.png]
code-corrected.png
[Thumbnail for code-corrected.png]
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
Either you didn't save your HTML file, you didn't reload it in your browser, or your browser is serving a cached version. The latter is unlikely.
 
John Ken
Ranch Foreman
Posts: 373
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok,may be i would not have saved it properly ,just saveed it and again invoked the same local host/v1/ on browser but this time error is:-

"Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) in F:\Java Training\SW\Netbeans\Xampp installation location\htdocs\v1\aditem.php on line 18"
 
Saloon Keeper
Posts: 6385
158
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Report post to moderator
Well, we don't know what the contents of that file currently are, so we can't guess what might be wrong, but surely you can figure out a syntax error now that you know on which line it occurs, if you examine it carefully and pay attention to every character. For example, looking at line 18 of the code posted above, it's missing a closing parentheses.

If you don't see it, even after carefully looking at it, and thinking about it, simplify the code - remove parameters, remove methods, etc. until it works. Then you know what went wrong, and can add back what you removed.
 
Stephan van Hulst
Saloon Keeper
Posts: 11899
253
  • Mark post as helpful
  • send pies
  • Report post to moderator
WITHOUT looking at the code you already have, and WITHOUT copying anything from anywhere, write a simple class named Greeter that has:

  • one field of type string named $subject and
  • a constructor that initializes the $subject field from a constructor parameter.

  • That is all, and nothing more.

    If you get stuck, consult the PHP Manual.

    When you are done, post your code here so we can review and continue to the next step.

    If it's obvious to us that you copied any code instead of thinking about it and writing it yourself, I'm going to lock this topic and any similar ones you will make after this.
     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    here is the desired code:-

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11899
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Good, it seems you know how to write a constructor. Now write a second class named Person. Use type hints to make sure that the $subject field and the $subject constructor parameter are of type Person.

    A person should have a string field $name and a constructor that takes a $name.

    Change your program entry point to create an instance of type Person, and also create an instance of type Greeter that takes the Person instance as its constructor argument.
     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    it seems instead of getting answer of my tough problem i have to solve a tougher problem now?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11899
    253
    • Likes 1
    • Mark post as helpful
    • send pies
    • Report post to moderator
    No. It is a simpler problem that you must understand before you can solve your real problem, which is more difficult.
     
    Tim Moores
    Saloon Keeper
    Posts: 6385
    158
    Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
    • Mark post as helpful
    • send pies
    • Report post to moderator
    John, in case you're wondering why Stephan is doing this: he is guiding you towards learning PHP. Learning is what this site is about: not simply giving out answers, but teaching people what they need to know to answer their own questions. It's the old principle of teaching someone to fish, instead of just giving them a fish.
     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    ok,

    If I understand did you mean below code was desired here ?

     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    sorry please ignore my previous code,what i understood according to that below code was desired:-

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11899
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You still need to create an instance of Greeter and pass it the instance of Person as its constructor argument.

    Add a method greetSubject() to Greeter that returns a greeting such as "Hello John!", where you obviously must use the name of the person in the greeting.
     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    i am not sure how could i achieve this just could try like below:-

    could you please provide the desired code for the same as i tried but could not write it as per desired goal.


     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11899
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator

    John Ken wrote:could you please provide the desired code for the same as i tried but could not write it as per desired goal.


    No. You already know everything that you need in order to be able to solve this task:

  • You know how to create different classes of different types.
  • You know how to create an instance of a type.
  • You know how to pass arguments to constructors.
  • You know how to call functions to get information from an object.

  • It's really easy:

    1) Create an instance of class Person by passing a name to its constructor.
    2) Create an instance of class Greeter by passing the Person instance from step 1 to its constructor.
    3) Call the greetSubject() method on the Greeter instance from step 2.
    4) The greetSubject() method uses the getName() method on its own $subject field.
     
    John Ken
    Ranch Foreman
    Posts: 373
    • Mark post as helpful
    • send pies
    • Report post to moderator
    ok,was below the one desired:-

     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11899
    253
    • Mark post as helpful
    • send pies
    • Report post to moderator
    You got it.

    Make sure to make all your fields private. You should also use type hints for method/constructor parameters and method return types.

    Anyway, we'll now move on to make the object graph a little bit more complicated.

    Get rid of the Greeter class. Instead, create a class Book that has two fields:

  • A field $title of type string.
  • A field $author of type Person.

  • Add a function __toString() that returns a string containing the title of the book followed by the author's name, separated by the string ' by '.

    Make it so I can run the following code:

    It should output the following message:
     
    That feels good. Thanks. Here's a tiny ad:
    Two software engineers solve most of the world's problems in one K&R sized book
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic