www.webdeveloper.com
Results 1 to 5 of 5

Thread: Help using AJAX/Javascript to change PHP variable.

  1. #1
    Join Date
    Aug 2012
    Location
    Milwaukee
    Posts
    15

    Help using AJAX/Javascript to change PHP variable.

    Been stuck on this for quite a bit... main problem is I'm very much a rookie when it comes to PHP and server side languages.

    But when this javascript function is completed:


    Code:
    request.done(function(msg)
    	{
    		if (msg.result === 0)
    		{
    			alert("The credit card was charged successfully!");
    		}

    I would like this PHP variable, which is in a different file, to change.

    Code:
    <?php if($row['membership_id'] == 0) :?>
    Any help would be greatly appreciated.
    Last edited by L3O; 02-23-2013 at 11:32 AM. Reason: accuracy

  2. #2
    Join Date
    Jan 2012
    Posts
    31
    PHP does not work like Javascript. For Javascript as long as the user has the page open you can modify variables. But for PHP once it has run it terminates and to run it again (like for an AJAX request) it has to reinitialize everything. PHP runs on request and then ends.

    If you wish to change that variable you would have to use PHP to edit the file, which is bad technique. This is why databases are used so you can store, change and then retrieve informtaion. Also you could use sessions to temporarily store a value while the user is on the page, but Javascript can do this also. The other option is to use a cookie to store the value then it would be available to just that one user across multiple sessions. But if you need that value to change and be available to multiple users you should use a database.

  3. #3
    Join Date
    Aug 2012
    Location
    Milwaukee
    Posts
    15
    Thanks for the great info.

    Is it possible to "activate" or redirect to a php page which will change the variable once the javascript function is done?

    Or better question... how can I change the php membership variable based on the success of the credit card transaction?

    And how would I go about using a database to change the variable? I know the membership package installed uses a database which was created.... but accessing that, or changing that is something I am very limited in.

    Sorry for all the questions.... This is the last thing I have to do before I get paid for the site!!!! And its the only thing I can't seem to complete.
    Last edited by L3O; 02-24-2013 at 12:00 AM.

  4. #4
    Join Date
    Aug 2012
    Location
    Milwaukee
    Posts
    15
    How about after they click Ok, javascript redirects them to a php page which changes the varaible onload... and the php page is protected by the membership level? Would that work?

  5. #5
    Join Date
    Aug 2012
    Location
    Milwaukee
    Posts
    15
    When I go into my Control Panel for the website in phpMyAdmin and browse the database variables I can see the variable I would like to change....

    What is the best way to change this variable after the previous javascript event occurs?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles