Jump to content

JS PHP Trouble


Trace7r

Recommended Posts

Ok i'm totaly new here, I play with some code when I have time for it... But I'm a little confused, it seems that the new ajax way is coded as

$.ajax

but that I don't understand yet,

 

Because i'm adjusting some existing code I have to go from one php file to another over js the old way...

 

So I have a php file where I echo a div with an input as type="text" and id="txtnameposition", in the same div I have a button who is calling the function closeandmail in the JS file.

 

In the JS file I get de input of the textbox as:

var jsName = document.getElementById("txtnameposition").value;

When I put an alert(jsName); inside the JS file I do get the input value, so passing values from the first php file to the js file is no problem....

 

BUT, to send this all to the next PHP file I used next code in the JS file:

function getHTTPObject(){var xhr = false;//set to false, so if it fails, do nothingif(window.XMLHttpRequest) {//detect to see if browser allows this methodvar xhr = new XMLHttpRequest();//set var the new request} else if(window.ActiveXObject) {//detect to see if browser allows this methodtry {var xhr = new ActiveXObject("Msxml2.XMLHTTP");//try this method first} catch(e) {//if it fails move onto the nexttry {var xhr = new ActiveXObject("Microsoft.XMLHTTP");//try this method next} catch(e) {//if that also fails return false.xhr = false;}}}return xhr;//return the value of xhr}function closeandmail(){var jsName = document.getElementById("txtnameposition").value;httpObjectMail = getHTTPObject();if (httpObjectMail !== null) {httpObjectMail.open("GET", "mail.php?"+ "Name=" + jsName);httpObjectMail.send(null);}httpObjectMail = null;}

And in the second PHP file I put next code:

$Name = $_GET['Name']; echo 'Name = '.$Name;$MailMessage = 'The name = '.$Name;$to      = 'mail@mail.com';$subject = 'The name = '.$Name;$headers = 'From: mail@mail.com'.'rn'.'X-Mailer: PHP/' . phpversion();mail($to, $subject, $MailMessage, $headers);

The echo only shows "Name = " and no value, so can someone please tell me what is wrong in this code? because I did try a lot of things but it didn't work so far.

 

Greetings Trace7r.

 

Link to comment
Share on other sites

Use your browser's developer tools to look at the ajax request to make sure it's sending the value on the querystring correctly. You can also use print_r($_GET) to print all of $_GET to see what's in it.

 

How do you know it is printing the empty name? Are you already using developer tools to look at the request?

Link to comment
Share on other sites

This is a module in a Joomla website and the echo is shown in de position where the module is running in.

 

I do know the "inspect element" screen if you right click on a website, but I don't know all the functionality, I did use firefox because it has a nice debug tool, but firefox doesn't work anymore on my PC.

I usualy check my work in the browsers I installed (Chrome "standard", IE, Opera, Safari, Firefox (not anymore)).

Did a new install of FF but still crashes, I have to admit that I didn't google for a solution yet on the FF problem but that's off-topic here :-)

 

Strange thing is also that the code is copied from a previous module I made, so I think it has to be a syntax problem but I can't find it, it did work once, so I zipped the code again to reinstall it, so I would know all was working well.

And then it didn't react anymore, the module does everything until I wanna get my data back out of the JS file to the PHP file.

Did put alerts in the js so I did see the code running with all var filled as it should be but the echo's in the mail.php like:

echo"Name = ".$Name;

only showed "Name = " in the site position running the module.

 

One thing I did struggle with is that the mail.php is in the main dir and the JS file is in a folder "JS" so the call for mail.php in the js-file should be like

httpObjectMail.open("GET", "../mail.php?"

So the Js file will search the mail.php file a level higher, I did not show that in the code above, because I didn't really think that would be the problem

 

Greets Trace7r

Link to comment
Share on other sites

Chome, Opera, Safari, and IE all have built-in developer tools. There are links in my signature if you want to look at them.

 

 

 

This is a module in a Joomla website and the echo is shown in de position where the module is running in.

There's something I'm missing, then. The Javascript code you posted does not do anything with the server response, it would not show the output from echo anywhere.

 

If the PHP code is not printing the value from $_GET, then there is nothing in $_GET. I'm not sure what else to say. That's why you need to look at the request, to verify what is happening.

Link to comment
Share on other sites

It seemed to be a syntax error just as I tought, the link to the PHP file had to start from the root up and most important of all, I did have to use the right ' or "...

OK Shame on me, but I did look over it, so I hope will not struggle that long again over something silly like that...

 

Justsomeguy thx for your help.

 

Grtz Trace7r.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...