The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> action attribute
tudsy
post Sep 25 2016, 01:33 AM
Post #1


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

A new problem has arisen. I am trying to construct a query string with a mixture of php variables and javascript variables in a form.

I have started to do this but its not working. The main file is ecovib2d.php and the processing file process.php.

Process.php is where I accept the parameters.

Another problem is that (maybe) is to use javascript variabes in php variabes.


Any help will be appreciated.Attached File  process__1_.php ( 4.67k ) Number of downloads: 678
Attached File  ecovib2d1.php ( 7.94k ) Number of downloads: 671
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 25 2016, 11:23 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I'm confused. You say 'process.php' will process the form data. But, I see you are using javascript to change the form action. Example:
CODE
document.getElementById('form').action.value =
"http://103.226.223.161/~ecovibdc/ECOVIB2D/MYART/process.php?$Name=username&$Email=email&Person=" + personsnamejs + "&Price=" + Priceofart + "&Graphic=" + selectedgraphicfilename;
I see 2 problems with the above line of code.
Problem 1:
CODE
document.getElementById('form').action.value
Should be:
CODE
document.getElementById('form').action
Where did you come up with the '.value' part?

Problem 2: Your query string is not constructed correctly. If 'username' and 'email' are javascript variables then you need to do what you did for 'Person=', 'Price=', etc... Are '$Name' and '$Email' PHP variables? Or are they just part of the name for that parameter?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Sep 25 2016, 02:02 PM
Post #3


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

Thanks for that.

$Name and $Email are PHP variables from the form.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Sep 25 2016, 05:32 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



There are several HTML errors that the validator will show.

I also recommend a more structured code formatting, for readability.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 25 2016, 10:30 PM
Post #5


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(tudsy @ Sep 25 2016, 02:02 PM) *

Hi

Thanks for that.

$Name and $Email are PHP variables from the form.

Are you sure it shouldn't be '?username=$Name&email=$Email' instead of what you have: '?$Name=username&$Email=email'?
Anyway, when javascript runs this line of code PHP has long since finished and died. Remember, PHP is server-side and runs when the web server serves up the page. Javascript is client-side and runs from the local device.

You could try:
CODE
?username=<?php echo {$Name};?>&email=<?php echo {$Email};?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Sep 26 2016, 03:55 AM
Post #6


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



QUOTE(CharlesEF @ Sep 26 2016, 01:00 PM) *

QUOTE(tudsy @ Sep 25 2016, 02:02 PM) *

Hi

Thanks for that.

$Name and $Email are PHP variables from the form.

Are you sure it shouldn't be '?username=$Name&email=$Email' instead of what you have: '?$Name=username&$Email=email'?
Anyway, when javascript runs this line of code PHP has long since finished and died. Remember, PHP is server-side and runs when the web server serves up the page. Javascript is client-side and runs from the local device.

You could try:
CODE
?username=<?php echo {$Name};?>&email=<?php echo {$Email};?>



Hi

Thanks for that.

The next question is what do I put, if anything, in the action attribute of the form?

I m using onsubmit to set the action attribute.

Thanks.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 26 2016, 12:58 PM
Post #7


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



QUOTE(tudsy @ Sep 26 2016, 03:55 AM) *

QUOTE(CharlesEF @ Sep 26 2016, 01:00 PM) *

QUOTE(tudsy @ Sep 25 2016, 02:02 PM) *

Hi

Thanks for that.

$Name and $Email are PHP variables from the form.

Are you sure it shouldn't be '?username=$Name&email=$Email' instead of what you have: '?$Name=username&$Email=email'?
Anyway, when javascript runs this line of code PHP has long since finished and died. Remember, PHP is server-side and runs when the web server serves up the page. Javascript is client-side and runs from the local device.

You could try:
CODE
?username=<?php echo {$Name};?>&email=<?php echo {$Email};?>



Hi

Thanks for that.

The next question is what do I put, if anything, in the action attribute of the form?

I m using onsubmit to set the action attribute.

Thanks.

Are you talking about the 'action' attribute of the <form> tag? Since you will use javascript to change the action attribute I would say it doesn't matter. You could put a dummy file name in the action attribute OR leave it blank (I think that's valid in HTML5).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Sep 27 2016, 04:13 PM
Post #8


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



QUOTE(CharlesEF @ Sep 27 2016, 03:28 AM) *

QUOTE(tudsy @ Sep 26 2016, 03:55 AM) *

QUOTE(CharlesEF @ Sep 26 2016, 01:00 PM) *

QUOTE(tudsy @ Sep 25 2016, 02:02 PM) *

Hi

Thanks for that.

$Name and $Email are PHP variables from the form.

Are you sure it shouldn't be '?username=$Name&email=$Email' instead of what you have: '?$Name=username&$Email=email'?
Anyway, when javascript runs this line of code PHP has long since finished and died. Remember, PHP is server-side and runs when the web server serves up the page. Javascript is client-side and runs from the local device.

You could try:
CODE
?username=<?php echo {$Name};?>&email=<?php echo {$Email};?>



Hi

Thanks for that.

The next question is what do I put, if anything, in the action attribute of the form?

I m using onsubmit to set the action attribute.

Thanks.

Are you talking about the 'action' attribute of the <form> tag? Since you will use javascript to change the action attribute I would say it doesn't matter. You could put a dummy file name in the action attribute OR leave it blank (I think that's valid in HTML5).


Hi

Thanks for that.

Attached File  ecovib2d1.php ( 8.37k ) Number of downloads: 625
I left the action attribute blank but when I executed the form from the submit button, it just reloaded the page.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Sep 28 2016, 10:09 AM
Post #9


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I have not had time to go over your new code. But, when you leave the action blank, it is supposed to load the same page upon submit. If there are times when the action is not replaced by javascript then you should have a default page in the action attribute. To handle the times when javascript doesn't replace the action.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Sep 28 2016, 02:03 PM
Post #10


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(CharlesEF @ Sep 26 2016, 07:58 PM) *

You could put a dummy file name in the action attribute OR leave it blank (I think that's valid in HTML5).

No HTML5 doesn't seem to allow an empty ACTION value (like HTML4), instead you can leave out the ACTION attribute altogether:

"The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces."
https://www.w3.org/TR/2014/REC-html5-201410...form-submission

(my emphasis). But a bit further down the spec says:

"The action IDL attribute must reflect the content attribute of the same name, except that on getting, when the content attribute is missing or its value is the empty string, the document's address must be returned instead."

(my emphasis again). Maybe I'm misunderstanding this. wacko.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Sep 30 2016, 01:00 AM
Post #11


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

Thanks for that.

I put process.php in the action attribute and set the action attribute in the function set_action().

In process.php, I am trying to set PHP variables with JavaScript values. I know you cannot do that but I was wondering if there was a way?

Jsvscript:

<script>
var gph = getQueryVariable2('Graphic');
var pri=getQueryVariable2('Price');
</script>

PHP:

echo $graphic=gph ;
$dateof=localtime();
echo $Price = pri;

Also localtime() returns a zero time?

Thanks.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Sep 30 2016, 06:19 AM
Post #12


.
********

Group: WDG Moderators
Posts: 9,653
Joined: 10-August 06
Member No.: 7



QUOTE(tudsy @ Sep 30 2016, 08:00 AM) *

I am trying to set PHP variables with JavaScript values. I know you cannot do that but I was wondering if there was a way?

You can let javascript change a form field value, which can then be submitted by the user to the PHP script. Or you can let javascript load a page (or create a link) with a URL querystring made by javascript. Both these methods require loading a new page.

To avoid loading a new page, you might use Ajax instead.


User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Oct 2 2016, 12:14 PM
Post #13


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



QUOTE(Christian J @ Sep 30 2016, 08:49 PM) *

QUOTE(tudsy @ Sep 30 2016, 08:00 AM) *

I am trying to set PHP variables with JavaScript values. I know you cannot do that but I was wondering if there was a way?

You can let javascript change a form field value, which can then be submitted by the user to the PHP script. Or you can let javascript load a page (or create a link) with a URL querystring made by javascript. Both these methods require loading a new page.

To avoid loading a new page, you might use Ajax instead.



Hi

Thanks for that. I am importing query.js (txt) from ecovib2d1.php but when its run in the browser, the BUY* button does not redirect to process.php. My knowledge of AJAX and how to use it is limited.

Any help is appreciated.

Thanks.


Attached File(s)
Attached File  ecovib2d1.php ( 7.92k ) Number of downloads: 373
Attached File  process.php ( 4.58k ) Number of downloads: 389
Attached File  query.txt ( 296bytes ) Number of downloads: 350
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Oct 10 2016, 06:26 AM
Post #14


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Hi

I have made progress in passing Price and Person but not Graphic values in the query string.

Attached is the latest code for my application.

Any help will be appreciated.


Attached File(s)
Attached File  process.php ( 4.6k ) Number of downloads: 367
Attached File  ecovib2d1.php ( 8.32k ) Number of downloads: 390
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 12 2016, 07:33 PM
Post #15


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Your approach really makes no sense to me. I'm confused as to why you want to use javascript instead of PHP in 'process.php'. I see you plan to send an email with a request to be paid through PayPal. How do you plan on sending this email? The email should be sent from the server side with PHP. Also, I don't see how 'process.php' can read any query string values. I say this because it looks like the 'action' attribute of the <form> element is malformed. I see 2 problems, I think.
Problem 1: The 'action' attribute value is an un-terminated string, meaning there is no closing quote.
Problem 2: I don't think you can use javascript variables or <script> tags inside an HTML attribute, in this case the 'action' attribute of the <form> tag.

If you really want to continue doing it your way then I suggest you create an onload function to be run after the page loads. In this function you should build the url string as you want then assign that value to the 'action' attribute of the <form> element.

I really really think you should use PHP in the 'process.php' page. This way you can get the POST values for each field in the <form>. You would not need the query string part of the url at all.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 13 2016, 09:34 PM
Post #16


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



After thinking about it, you don't want to place it in the onload function at all. It needs to be placed in the 'changePic' function. In the 'ecovib2d1.php' code you attached, make these 2 changes.

HTML
Change the opening <form> element to this:
CODE
<form  id = 'form' action="" method="POST"/>

Javascript
Add this 1 line of code before the return statement in the 'changePic' function, like this:
CODE
      }
   document.getElementById('form').action = "process.php?$Name=username&$Email=email&Person="+ personsnamejs + "&Price=" + Priceofart + "&Graphic=" + picname.replace('w.bmp','.bmp');
   return picname;

Does that work for you?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Oct 16 2016, 01:56 PM
Post #17


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Thanks for that. The username and email work but how do I process the JavaScript variables in the query string?

Thanks again.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 16 2016, 05:12 PM
Post #18


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I've said this before, I think "$Name=username&$Email=email" should be "username=$Name&email=$Email". This way you can use "getQueryVariable2('username')" and "getQueryVariable2('email')" to get the values.

Also, it would be better to place that 1 line of code I gave you before inside the "newImage.onload" function, like this:
CODE
function changePic(picname,personname)
{
  if(picname != 'default')
  {
    var path = 'http://103.226.223.161/~ecovibdc/ECOVIB2D/MYART/' +  personname + '/Graphicw/' + picname;
    var myimg = document.getElementById('myImage');
    var newImage = new Image();
    newImage.src = path;
    newImage.onerror = function()
    {
      alert('There was a problem loading the Image file! - Please try again');
      myimg.style.height = '';
      myimg.src = '';
    }
    newImage.onload = function()
    {
      myimg.src = path;
      document.getElementById('form').action = "process.php?username=$Name&email=$Email&Person="+ personsnamejs + "&Price=" + Priceofart + "&Graphic=" + picname.replace('w.bmp','.bmp');
      }
      }
   return picname;
};
I'm not even sure why you have "return picname;" in the function. In the first place, it is in the wrong place. In the second place, you don't assign that value to any variable, so why have it?

This post has been edited by CharlesEF: Oct 16 2016, 05:24 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
tudsy
post Oct 18 2016, 07:55 AM
Post #19


Advanced Member
****

Group: Members
Posts: 246
Joined: 30-September 14
Member No.: 21,611



Thanks for that. It works with getQueryVariable2.

Is this piece of code correct?

var a = document.getElementById('download');
a.href='http://103.226.223.161/~ecovibdc/ECOVIB2D/MYART/' + Person + '/Graphic/' + Graphic;
where

<a id= 'download' href="" onclick='Send_Email()'><p align='center' id='dlink'></a></p>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Oct 18 2016, 08:56 AM
Post #20


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



The javascript looks fine to me. The HTML is invalid because your closing tags are in the wrong order. I mean, you start with an <a> then a <p> so the first closing should be </p> then </a>.

I still think you would be better off if you redesign the layout. Start using PHP more and Javascript less.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 19th April 2024 - 09:35 AM