need to send data in to php file from html form

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 5391 Views - Last Post: 09 July 2011 - 09:05 AM Rate Topic: -----

#1 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

need to send data in to php file from html form

Posted 08 July 2011 - 04:49 AM

hi all,

I am new to php. I want to passe data from html page to php file.

here is the code of createComment.html


    <form action="commentSave.php" method="POST">
       <label>Name</label> <input type="text" name="name" value="" /><br>
       <label>Email</label><input type="text" name="email" value="" /><br>
    <label> Website</label><input type="text" name="web" value="" /><br>
        <?php
        echo $var="menuka";
        ?>
   <textarea id="markItUp" cols="80" rows="20" name="comment">

</textarea>

    <input type="submit" value="Submit" />
    </form>



what I tried was print the value of $var in the commentsave.php file

i got all the other values except $var.

i am trying this because I am developing a blog where i want to reply administrator's thread. so i get the thread ID of the thread and passe it with the form data of comments. so that i can read it back properly i.e thread and relevant comments from the data base.
that is why i am asking how to pass the value of $var to php file....
I think you can understand me. please somebody help me.

thanks in advance,
menuka

Is This A Good Question/Topic? 0
  • +

Replies To: need to send data in to php file from html form

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,590
  • Joined: 08-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 04:56 AM

line 5 - 7 of the HTML file are just text. text is not passed to PHP (why should it?) only form values (like those from <input> and <textarea>).
Was This Post Helpful? 1
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 05:49 AM

You can place the value in a hidden input field.
<input type="hidden" name="id" value="your_value" />

Was This Post Helpful? 1
  • +
  • -

#4 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 07:27 AM

thx all it works.

i have to clear how if u want to pass value of id in a php file which taken from database to another page using

<a href="Blog/createComments.php"/>


or if i have more variable such as $var1 , $var2 ,$var3 then how can i pass it to another php file


thx inadvace menuka
Was This Post Helpful? 0
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 07:29 AM

If you're passing values from one PHP file to another then use a SESSION.
Was This Post Helpful? 1
  • +
  • -

#6 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 07:34 AM

is there any option other than session.

Is there any option to pases values via the URL because i get values from the database to the variable . therefore i think session is not good for that.

thx in advance,

menuka
Was This Post Helpful? 0
  • +
  • -

#7 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 07:34 AM

You could also pass it via the URL provided using GET requests. Just make sure it's not super secure or important variables that you pass via GET requests.
http://www.w3schools...php/php_get.asp
Was This Post Helpful? 0
  • +
  • -

#8 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 08:38 AM

hi all ,

i know how to passe values using get and post .

i want to passe values using <a href= .../>


attached the variable value and passe it to another page


thx in advance,
menuka
Was This Post Helpful? 0
  • +
  • -

#9 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,758
  • Joined: 08-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 08:55 AM

View Postmenukadevinda, on 08 July 2011 - 03:38 PM, said:

i know how to passe values using get and post .

i want to passe values using <a href= .../>

You can do things like this:
<a href="page.php?var1=val1&amp;var2=val2&amp;var3=val3">Linkage</a>

Which would link you to a page with the URL:
- page.php?var1=val1&var2=val2&var3=val3

This is what we call GET variables. In the page.php code you could fetch the GET values using the $_GET array.
<?php
echo $_GET['var1']; // == "val1"
echo $_GET['var2']; // == "val2"
echo $_GET['var3']; // == "val3"
?>


This post has been edited by Atli: 08 July 2011 - 08:55 AM

Was This Post Helpful? 1
  • +
  • -

#10 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 469
  • Joined: 27-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 08:58 AM

Yes, you use GET requests.

Link to learn all about them:
http://www.w3schools...PHP/php_get.asp

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#11 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 09:07 AM

hi all,


<?php
$val1=$row['postId'];
          echo '<a href="Blog/createComments.php?var1=val1;">'.'leave comment'.'</a>'.'<br>';
?>



here is the code

and i got this result
value of var isval1

it is obvious i want not the val1 but the value of $val1 variable.

i think u can understand me.

thx in advance,
menuka
Was This Post Helpful? 0
  • +
  • -

#12 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,758
  • Joined: 08-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 09:10 AM

View Postmaniacalsounds, on 08 July 2011 - 03:58 PM, said:

Link to learn all about them:
http://www.w3schools...PHP/php_get.asp

w3schools.com said:

The built-in $_GET function is used to collect values in a form with method="get"

Hmmm... $_GET is not a function, it's an array; a super-global. Also, this gives the impression that you need a form to use GET, which isn't true.

This is why I've stopped linking to W3Schools. They tend to have misleading, if not flat out wrong, info.

Better to just link to the PHP manual :)
Was This Post Helpful? 1
  • +
  • -

#13 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,758
  • Joined: 08-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 09:16 AM

View Postmenukadevinda, on 08 July 2011 - 04:07 PM, said:

it is obvious i want not the val1 but the value of $val1 variable.

It's obvious to us, but if you want PHP to use the variable $val1, you need to include the $ char. Without it, PHP will just read val1 as a normal string.

If you want to inject a variable into a string, you have two options.

First, to end the string, concat it with the variable, and then reopen the string to add the rest.
$val1 = "Hello";
$str = 'Value of val1 is ' . $val1 . '.';
// Value of val1 is Hello.


Or to use double-quotes, and just put the variable into the string.
$val1 = "Hello";
$str = "Value of val1 is $val1.";
// Value of val1 is Hello.


NOTE! These must be double-quotes ("..."), not single-quotes ('...'). PHP does not look for variables inside single-quoted strings.
Was This Post Helpful? 0
  • +
  • -

#14 menukadevinda  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 470
  • Joined: 14-April 11

Re: need to send data in to php file from html form

Posted 08 July 2011 - 09:45 AM

still i get the same result.

here is the full code.

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
         <STYLE type="text/css">
   .blog{float:left;
            width:800px;
            font-family:Verdana, Geneva, sans-serif;
   }
   .col1,.col2{
       float:left;
width:600px;
   }
   .clear{
       clear:both;
   }
 </STYLE>

    </head>
    <body>
    <div class="blog">

    <div class="col1">
     <?php
        //data will be taken from the database and will be printed in the blog page to
        //users to view.
         include '/Classes/DBhandling.php';
        $dob = new Dbhandling('localhost','root','classdb','######');
        $dob->connectDB();

//this query is used to select posts and relevent comments if the administrator
//has given the permistion
  
 $qry1 ="SELECT thread.title,thread.id, thread.post, thread.datePosted, userComments.name_cm,
 userComments.comment_cm, userComments.commentId, userComments.allow_cm, userComments.dateComment_cm
FROM userComments
RIGHT JOIN thread ON thread.id = userComments.postId
ORDER BY userComments.dateComment_cm";

         $result=mysql_query($qry1);
         while($row=mysql_fetch_array($result)){
             echo '<h3>'.$row['title'].'</h3>';
             echo $row['post'].'<br>'.'<br>'.'<br>';
             echo "date posted ". $row['datePosted'].'<br>'.'<br>';
      //check whether post has a comment and administrator's permistion.
        if(!empty($row['commentId']) && $row['allow_cm']==1){
                     echo "&nbsp"."<br>"."reply by :".$row['name_cm'].'<br>';
                 echo "&nbsp"."the message is".$row['comment_cm'].'<br>';
                 echo "&nbsp".'date posted'.$row['dateComment_cm'];
                 echo 'commentID'.$row['commentId'].'<br>';
                 echo 'allow commetn'.$row['allow_cm'];
               //  echo "ID".$row['id']."<$> ".$row['title']." ".$row['popst'];
           }
        $val1=$row['id'];
 //echo $val1;
//$val1 = "Hello";
$str = "Value of val1 is $val1.";
          echo '<a href="Blog/createComments.php?var1=str">'.'leave comment'.'</a>'.'<br>';
//<a href="page2c.php?myNumber=1&myFruit=orange">
//a href="page.php?var1=val1&amp;var2=val2&amp;var3=val3
         echo '<hr>';
         }
         $dob->closeDB();
        // put your code here
        ?>

    </div>
    <div class="col1" >
     </div>
    <div class="clear">

     </div>


        </div>
    </body>
</html>



cratecommet.html

<?php
        // put your code here

echo 'value of var is'.$_GET['var1'];
  ?>
<form action="commentSave.php" method="POST">
       <label>Name</label> <input type="text" name="name" value="" /><br>
       <label>Email</label><input type="text" name="email" value="" /><br>
    <label> Website</label><input type="text" name="web" value="" /><br>
        <input type="hidden" name="id" value="your_value" />
   <textarea id="markItUp" cols="80" rows="20" name="comment">

</textarea>

    <input type="submit" value="Submit" />
 </form>




thx in adavace
menuka
Was This Post Helpful? 0
  • +
  • -

#15 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,590
  • Joined: 08-June 10

Re: need to send data in to php file from html form

Posted 08 July 2011 - 09:50 AM

did I mention that PHP code inside a HTML file is just text and is not interpreted by PHP? (unless you have configured your webserver to feed every HTML file, may it contain PHP code or not, to the PHP interpreter)

This post has been edited by Dormilich: 08 July 2011 - 09:51 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2