Help - Search - Members - Calendar
Full Version: action attribute
HTMLHelp Forums > Programming > Server-side Scripting
tudsy
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.Click to view attachment Click to view attachment
CharlesEF
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?
tudsy
Hi

Thanks for that.

$Name and $Email are PHP variables from the form.
Christian J
There are several HTML errors that the validator will show.

I also recommend a more structured code formatting, for readability.
CharlesEF
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};?>

tudsy
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.
CharlesEF
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).
tudsy
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.

Click to view attachmentI left the action attribute blank but when I executed the form from the submit button, it just reloaded the page.
CharlesEF
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.
Christian J
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
tudsy
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.
Christian J
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.


tudsy
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.
tudsy
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.
CharlesEF
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.
CharlesEF
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?
tudsy
Thanks for that. The username and email work but how do I process the JavaScript variables in the query string?

Thanks again.
CharlesEF
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?
tudsy
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>
CharlesEF
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.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2019 Invision Power Services, Inc.