Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calling a php script from javascript onclick

    i want to execute a php script when i click on a button in html generated by javascript,i was trying with jquery ajax and i did this but nothing happens...any help pls?the php script is working so its not that,i guess i am missing something in this ajax call...



    Code:
    $(".formBtn").click(function(){
        
        $.ajax({
            url: "script to call",
            type: "post",
            
            // callback handler that will be called on success
            success: function(response, textStatus, jqXHR){
                // log a message to the console
                console.log("Hooray, it worked!");
                alert("Working!");
            },
            // callback handler that will be called on error
            error: function(jqXHR, textStatus, errorThrown){
                // log the error to the console
                console.log(
                    "The following error occured: "+
                    textStatus, errorThrown
                );
            },
            // callback handler that will be called on completion
            // which means, either on success or error
            complete: function(){
                // enable the inputs
                $inputs.removeAttr("disabled");
            }
        });
        
      });
    And here is the php script i want to call

    PHP Code:

        

        $api_key 
    'apikey';
        
    $project_id 'projectid';
        
    $phone_id 'phoneid';    
        
    $to_number 'number';
        
    $content 'content';
        
        
        
    $curl curl_init();
        
    curl_setopt($curlCURLOPT_URL
            
    "urlblblbll");
        
    curl_setopt($curlCURLOPT_USERPWD"{$api_key}:");  
        
    curl_setopt($curlCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($curlCURLOPT_POSTFIELDShttp_build_query(array(
            
    'content' => $content,
            
    'phone_id' => $phone_id,
            
    'to_number' => $to_number,
            
        )));        
        
       
        
        
    $json curl_exec($curl);    
        if (
    $err curl_error($curl)) { echo "$err\n"; }    
        
    curl_close($curl);    
        
        
    $res json_decode($jsontrue);        
        
        
    var_dump($res); // do something with $res
        


  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    108
    Thanks
    19
    Thanked 13 Times in 13 Posts
    the php script is working so its not that
    Try the JavaScript forum
    The CodingForums Funny Images thread ** Now with 20+ pages!! ** - http://www.codingforums.com/showthread.php?t=245405

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,684
    Thanks
    25
    Thanked 656 Times in 655 Posts
    console.log() kills things for me in normal web page. Think that's cause I'm not running this in firebug.

    Two things in the php is the final } does not belong
    and
    Code:
    Could not resolve host: urlblblbll; Host not found

  • #4
    New Coder
    Join Date
    Mar 2012
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    dont you need to put onClick not sclick in the first line of javascript


  •  

    Posting Permissions

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