Can javascript function send its argument data to PHP script within the same function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sufian
    New Member
    • Jul 2006
    • 9

    #1

    Can javascript function send its argument data to PHP script within the same function

    <input type="image" id="imageField " class="btn" src="<?php bloginfo('templ ate_url'); ?>/media/global/btn-go.gif" onclick = "sendRequestPos t(document.getE lementById('ema il1').value);" />
    The function sendRequestPost () is in a file named header.php:
    function sendRequestPost (data) {
    // Open PHP script for requests

    MM_validateForm ('email1','','R isEmail');

    if (document.MM_re turnValue) {

    <?php
    $email = ??????????????? ??????????????? ????
    $subject = 'The Second Floor: Test 000 Welcome!';
    $message = 'Greetings!'. "\r\n\r\n";
    $message .= 'Thank you for signing up for updates via the T2F website. We will keep you posted on events, new coffee concoctions, and fresh book titles.'. "\r\n\r\n";
    $message .= 'We look forward to seeing you soon.'. "\r\n\r\n";
    $message .= 'Peace!'. "\r\n";
    $message .= 'Sabeen'. "\r\n\r\n";
    $message .= 'http://www.t2f.biz'. "\r\n";
    $message .= 'http://www.t2f.biz/t2f-events'. "\r\n";
    $message .= 'http://www.t2f.biz/blog';
    $headers = 'From: Sabeen Mahmud <info@t2f.biz>' . "\n";
    $headers .= 'Bcc: mdanishp9@yahoo .com';
    mail($email, $subject, $message, $headers);
    ?>


    http.open('post ', phpscript);
    http.setRequest Header('Content-Type', 'application/x-www-form-urlencoded');
    http.onreadysta techange = handleResponseP ost;
    http.send('emai l1='+data);
    }
    }

    How can I use the argument value of the function to send email. Thx
    The file subscribe.php is as under:
    <?php
    /* Short and sweet */

    if (! isset($wp_did_h eader)):

    $wp_did_header = true;

    require_once( dirname(__FILE_ _) . '/wp-config.php');

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    global $wpdb;

    if(isset($_POST['email1'])){
    $email = $_POST['email1'];

    $query2 = "SELECT * FROM rs_em WHERE EMAIL = '".$email."' ";
    $found = $wpdb->get_var($query 2);

    if (!$found) {

    //<div id="message" class="success" ></div>
    echo("<div id=\"message\" class=\"success \">Thank you! We will keep you posted.</div>");
    /*
    $subject = 'The Second Floor: Test 000 Welcome!';
    $message = 'Greetings!'. "\r\n\r\n";
    $message .= 'Thank you for signing up for updates via the T2F website. We will keep you posted on events, new coffee concoctions, and fresh book titles.'. "\r\n\r\n";
    $message .= 'We look forward to seeing you soon.'. "\r\n\r\n";
    $message .= 'Peace!'. "\r\n";
    $message .= 'Sabeen'. "\r\n\r\n";
    $message .= 'http://www.t2f.biz'. "\r\n";
    $message .= 'http://www.t2f.biz/t2f-events'. "\r\n";
    $message .= 'http://www.t2f.biz/blog';
    $headers = 'From: Sabeen Mahmud <info@t2f.biz>' . "\n";
    $headers .= 'Bcc: mdanishp9@yahoo .com';
    mail($email, $subject, $message, $headers);
    */
    $query = "INSERT INTO rs_em (email) VALUES ('".$email."')" ;
    $wpdb->query($query );
    } else {
    // <div id="message" class="error"></dib>
    echo("<div id=\"message\" class=\"error\" >Ooops. We already have this <nobr>e-mail</nobr> address in our database. Please enter another address.</div>");
    }
    }
    endif;
    ?>
    Actually the Ajax message come after 8 to 9 seconds here and the same PHP mail function takes only 1.5 to 2 seconds when written in header.php but how can I use the email from $_POST here?
    Please help me, this is my first task as a web developer for the website (www.t2f.biz)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    sufian, welcome to TSDN!

    Do you want to make an AJAX post request? What do you mean by within the same function?

    Comment

    • sufian
      New Member
      • Jul 2006
      • 9

      #3
      Yes the AJAX message:
      echo("<div id=\"message\" class=\"success \">Thank you for signing up. An e-mail will be sent to your account shortly and we will keep you posted.</div>");
      is actually shown just below the text box but it took almost 8 to 9 seconds (as mentioned before) to display that message because the mail function in subscribe.php file takes a lot of time. When I comment the mail function in the following subscribe.php file it took only 1.5 to 2 seconds to display the message.
      This is subscribe.php:
      <?php

      if (! isset($wp_did_h eader)):
      $wp_did_header = true;
      $subject = 'The Second Floor: Welcome!';
      $message = 'Greetings!'. "\r\n\r\n";
      $message .= 'Thank you for signing up for updates via the T2F website. We will keep you posted on events, new coffee concoctions, and fresh book titles.'. "\r\n\r\n";
      $message .= 'We look forward to seeing you soon.'. "\r\n\r\n";
      $message .= 'Peace!'. "\r\n";
      $message .= 'Sabeen'. "\r\n\r\n";
      $message .= 'http://www.t2f.biz'. "\r\n";
      $message .= 'http://www.t2f.biz/t2f-events'. "\r\n";
      $message .= 'http://www.t2f.biz/blog';
      $headers = 'From: Sabeen Mahmud <info@t2f.biz>' . "\n";
      $headers .= 'Bcc: mdanishp9@yahoo .com';
      require_once( dirname(__FILE_ _) . '/wp-config.php');
      global $wpdb;
      if(isset($_POST['email1'])){
      $email = $_POST['email1'];
      $query2 = "SELECT * FROM rs_em WHERE EMAIL = '".$email."' ";
      $found = $wpdb->get_var($query 2);
      if (!$found) {
      echo("<div id=\"message\" class=\"success \">Thank you for signing up. An e-mail will be sent to your account shortly and we will keep you posted.</div>"); $query = "INSERT INTO rs_em (email) VALUES ('".$email."')" ;
      $wpdb->query($query );
      mail($email, $subject, $message, $headers); }
      else {
      echo("<div id=\"message\" class=\"error\" >Ooops. We already have this <nobr>e-mail</nobr> address in our database. Please enter another address.</div>");
      }
      }
      endif;
      ?>

      When I was facing this problem I have tried to write PHP code in the Javascript function sendRequestPost () in the header.php file.
      This is header.php file:


      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>

      <meta http-equiv="Content-Type" content="<?php bloginfo('html_ type'); ?>; charset=<?php bloginfo('chars et'); ?>" />
      <title><?php bloginfo('name' ); ?> <?php wp_title(); ?></title>
      <link href="<?php bloginfo('templ ate_url'); ?>/css/base.css" rel="stylesheet " type="text/css" />
      <link href="<?php bloginfo('templ ate_url'); if (is_home()) {echo('/style.css');} else { echo('/css/page.css');}?>" rel="stylesheet " type="text/css" />
      <link href="<?php bloginfo('templ ate_url'); ?>/css/subnav.css" rel="stylesheet " type="text/css">

      <script type="text/javascript" src="<?php bloginfo('templ ate_url'); ?>/js/base.js"></script>

      <!-- sIFR Implementation -->
      <?php require_once('s ifr.php'); ?>
      <script type="text/javascript" language="javas cript">


      // Set path to PHP script
      var phpscript = 'subscribe.php' ;

      function createRequestOb ject() {
      var req;
      if(window.XMLHt tpRequest){
      // Firefox, Safari, Opera...
      req = new XMLHttpRequest( );
      } else if(window.Activ eXObject) {
      // Internet Explorer 5+
      req = new ActiveXObject(" Microsoft.XMLHT TP");
      } else {
      // There is an error creating the object,
      // just as an old browser is being used.
      alert('There was a problem creating the XMLHttpRequest object');
      }
      return req;
      }

      // Make the XMLHttpRequest object
      var http = createRequestOb ject();

      function sendRequestGet( act) {
      // Open PHP script for requests
      http.open('get' , phpscript+'?ema il1='+act);
      http.onreadysta techange = handleResponseG et;
      http.send(null) ;
      }

      function sendRequestPost (data) {
      // Open PHP script for requests
      MM_validateForm ('email1','','R isEmail');
      if (document.MM_re turnValue) {

      ///////////////////////////////////help needed here ////////////////////////////////////////////////
      <?php
      $email = What abot this ??????????????? ???????????
      $subject = 'The Second Floor: Welcome!';
      $message = 'Greetings!'. "\r\n\r\n";
      $message .= 'Thank you for signing up for updates via the T2F website. We will keep you posted on events, new coffee concoctions, and fresh book titles.'. "\r\n\r\n";
      $message .= 'We look forward to seeing you soon.'. "\r\n\r\n";
      $message .= 'Peace!'. "\r\n";
      $message .= 'Sabeen'. "\r\n\r\n";
      $message .= 'http://www.t2f.biz'. "\r\n";
      $message .= 'http://www.t2f.biz/t2f-events'. "\r\n";
      $message .= 'http://www.t2f.biz/blog';
      $headers = 'From: Sabeen Mahmud <info@t2f.biz>' . "\n";
      $headers .= 'Bcc: mdanishp9@yahoo .com';
      mail($email, $subject, $message, $headers);
      ?>

      http.open('post ', phpscript);
      http.setRequest Header('Content-Type', 'application/x-www-form-urlencoded');
      http.onreadysta techange = handleResponseP ost;
      http.send('emai l1='+data);
      }
      }

      function handleResponseG et() {
      if(http.readySt ate == 4 && http.status == 200){
      // Text returned from PHP script
      var response = http.responseTe xt;
      if(response) {
      // Update ajaxTest content
      document.getEle mentById("dt"). innerHTML = response;
      }
      }
      }

      function handleResponseP ost() {
      if(http.readySt ate == 4 && http.status == 200){
      // Text returned from PHP script
      var response = http.responseTe xt;
      if(response) {
      // Update ajaxTest2 content
      document.getEle mentById("dt"). innerHTML = response;
      //alert(response) ;
      }
      }
      }

      </script>


      </head>

      <body>
      <div id="shadow-head"></div>
      <div id="shadow-body"><!--SITE WRAPPER STARTS (ends in footer.php)-->
      <div id="site-wrapper">
      <div id="head">
      <div id="hair">
      <h1><a href="<?php bloginfo('url') ?>">
      <span>The Second Floor</span>
      </a></h1>
      <p>coffee &bull; books &bull; conversation </p>

      </div>
      </div>


      Within the same function means: Can the body of a Javascript function contains the php code in which the Javascript's function argument value is passed as a PHP variable? Because it takes the email data as its argument from the form.

      Comment

      Working...