SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php include question

    Hi, anyone know if you can send a form action to a php include or php function instead of php elsewhere? So that it will execute code when submit is clicked? I am trying to get echo's to appear on the web page when a form is sent with php instead of black text on the blank page in server land. If anyone knows of a way to get an echo on the web page itself please let me know, thank you very much.

  2. #2
    An average geek earl-grey's Avatar
    Join Date
    Mar 2005
    Location
    Ukraine
    Posts
    1,403
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Hi, anyone know if you can send a form action to a php include or php function instead of php elsewhere?
    PHP Code:
    $_GET['submit'] = '1';
    include 
    'script.php'

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats a pretty nifty idea. What does the 1 do, where do I put it, how does it call the submit, and does one variable have to call on the other?

    $_GET['submit'] = <php include 'script.php'; ?>
    ?

    $_GET['$include'] = '1';
    $include = <php include 'script.php'; ?>


    I just want when a submit button is clicked on for the echo message to appear on the web page it is on. Instead of the blank white page that echo's normally appear on.

    All ideas please send. Thank you very much.

  4. #4
    Non-Member
    Join Date
    Apr 2006
    Location
    Scotland
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post

    I just want when a submit button is clicked on for the echo message to appear on the web page it is on. Instead of the blank white page that echo's normally appear on.
    Then thats a javascript thing, not php

  5. #5
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    Hi, anyone know if you can send a form action to a php include or php function instead of php elsewhere? So that it will execute code when submit is clicked? I am trying to get echo's to appear on the web page when a form is sent with php instead of black text on the blank page in server land. If anyone knows of a way to get an echo on the web page itself please let me know, thank you very much.
    JavaScript is the only way to create client-side effects (stuff that happens in the browser without going to a new page). Program whatever response you want to that button being clicked in JavaScript.

    If you must use server-side code (for example, if the response depends on information in a database), then you're into the realm of AJAX - asynchronoux javascript and XML. You instantiate an XMLHttpRequest JavaScript object, use it to send an HTTP request to your server, and process the response in JavaScript, allowing you to avoid the page reload to talk to the server.

    You'll find lots of tutorials and books if you search for AJAX.

  6. #6
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That makes sense about the javascript but aside from the ajax since thats going to be a lot more work if I don't need it.

    Since the submit button is within the php and not on a regular html page, I won't be able to access the submit button using javascript. Couldn't an echo be put into the php page so that it uses the echo on the page when the form is sent? I tried this but I think I have some quotes wrong, or the Post submit wrong.
    if (isset($_POST['submit']){echo 'email sent';}
    The error message says
    Parse error: unexpected '{'

    Here's all of it.

    <?php
    while ($row = mysql_fetch_array($result))
    //other stuff up here that works fine. That pops up this form.
    {
    echo "<form action='sendemail.php' method='post'>
    <input type='text' name='sendingfield' size=15>
    <input type='submit' name='submit' value='submit' /></form>"; }
    if (isset($_POST['submit']){echo 'email sent';}}?>

    And the sendemail.php on separate page that send the $another field to the email.

    <?php
    $mailto = 'emailaddress@whatever.com';
    $another = $_POST['sendingfield'];//field sending to email when submit is pushed
    $subject = 'Subject' for email;
    $uself = 0;
    $sep = (!isset( $uself ) &#166;&#166; ($uself == 0))? '\r\n' : '\n' ; //puts fields into email correctly.
    $messageproper = 'Filled field listed in email field: $another\n';
    mail($mailto, $subject, $messageproper, 'From:' . $sep . 'Reply-To:' . $sep);
    ?>

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 1Jen View Post
    That makes sense about the javascript but aside from the ajax since thats going to be a lot more work if I don't need it.

    Since the submit button is within the php and not on a regular html page, I won't be able to access the submit button using javascript.
    That's a misunderstanding of how the web fundamentally works. A submit button can't appear on your screen unless the browser has been sent HTML which tells it to be.

    PHP is what's called a "server side" language. Unlike JavaScript and VBScript, which are part of a webpage and are executed by the browser, PHP code resides on your web host's server and is executed on the server. When someone makes an HTTP request for a page with PHP code in it, the web server fires up the PHP interpreter, gives it the PHP code you wrote, the PHP interpreter executes that code, and returns its output to the web server, which then forwards that to the web browser that made the inital request.

    The output of your PHP program is HTML code. There is no PHP in what gets sent to the browser. Go to your PHP webpage in a browser, right click on the window, and choose "view source". You'll see only the HTML your PHP code provided as output. You'll also see that submit button you created.

    So, the submit button is there and available for JavaScript to make use of. If you want text to appear within the same page when the submit button is clicked, the only solution is JavaScript. That's not to say PHP can't be involved, but PHP's job has to be done before the page is displayed, unless you start doing AJAX.

    AJAX doesn't break this system -- it works the same way. The neat "trick" is that this XMLHttpRequest JavaScript object modern browsers have implemented allows JavaScript to issue another HTTP request to the server in the background, which gets processed the same as if you had typed another URL into your browser window, but the response goes back to the JavaScript for processing instead of directly to the screen.

    Quote Originally Posted by 1Jen View Post

    if (isset($_POST['submit']){echo 'email sent';}
    The error message says
    Parse error: unexpected '{'
    The error here is that you don't have the same number of right parentheses as left parentheses. Specifically, you didn't end the condition of the if statement.

  8. #8
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are absolutely right. I forgot the ending parentheses. After I look at all these after a long time, I can't see something simple.

    I am no longer getting an error message and the item is sent to my email. But the last echo not appearing on the page when submit is sent. Need an echo wiz. Anyone know how to do an echo when a submit button is pushed?

    <input type='submit' name='submit' value='submit' /></form>"; }
    if (isset($_POST['submit'])){echo 'email sent';}}?>

    If not, can you use javascript within php to do something on an onclick?
    There now the last one is there.
    Last edited by 1Jen; Jan 3, 2007 at 23:51.

  9. #9
    Non-Member
    Join Date
    Apr 2006
    Location
    Scotland
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like Dan said your if statment if wrong. Try swapping your "if" code with this.

    PHP Code:
    if (isset($_POST['submit'])) {
    echo 
    'Email sent';
    } else {
    echo 
    'Damn, there was an error';


  10. #10
    Non-Member
    Join Date
    Apr 2006
    Location
    Scotland
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok i decided to go do it all again. This is all one file called email.php

    PHP Code:
    <?php

    if (isset($_POST['message'])) {

    // they entered a message so lets send the email

    $to 'emailaddress@whatever.com';
    $subject 'Message from your site';
    $message $_POST['message'];

    mail($to$subject$message);

    echo 
    'Email sent';

    } else {

    // else they didnt enter a message yet so show the form

    ?>

    <form action="email.php" method="post">
    Enter your message below:<br />
    <textarea name="message"></textarea>
    <br /><br />
    <input type="submit" name="submit" value="submit" />
    </form>

    <?php
    }
    ?>

  11. #11
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I might need you again, I have to check out more on this php self and see if its even going to work. Otherwise I won't even be able to see the echo. I'll be back.

  12. #12
    SitePoint Addict
    Join Date
    May 2006
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I can incorporate this into what I need, maybe you guys can use this.

    <?php
    if($_POST['text']=="")
    {
    echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
    echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";
    }
    else
    {
    echo $_POST['text'];
    echo "<form method=\"post\" action=\"{$_SERVER['PHP_SELF']}\">";
    echo "<p><input type=\"text\" name=\"text\"><p><input type=\"submit\" value=\"submit\"></p></form>";

    }
    ?>

  13. #13
    Non-Member
    Join Date
    Apr 2006
    Location
    Scotland
    Posts
    325
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When using echo you should stick to using single quotes ( ' ) and not double quotes ( " ).

    Double quotes take more time/power to parse and mean that you have to put \ infront of al your html which you dont want. Instead of:
    echo "<form action=\"...
    Use this:
    echo '<form action="...

    Also when you are doing a big block of html with no php variables in it, you can close php and reopen it again. Like:

    some php here

    ?>

    lots of html with no php stuff needed to be parsed

    <?php

    more php

  14. #14
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mikexx2020 View Post
    When using echo you should stick to using single quotes ( ' ) and not double quotes ( " ).

    Double quotes take more time/power to parse and mean that you have to put \ infront of al your html which you dont want.
    We're talking nanosecond differences here


Bookmarks

Posting Permissions

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