Magento Forum

   
How do “form.phtml” and “cc.php” communicate? How are the variables passed? 
 
stereotomy
Jr. Member
 
Total Posts:  28
Joined:  2008-02-10
 

Okay, firstly, please forgive my ignorance, I cannot drill too much of the code..

Here goes my question:

On one hand, I have the FORM.PHTML file, which is the template.
On the other hand, I have the Modal file, which is the CC.PHP.

..and, I am trying to capture the CC details for the customer.

My question is: How exactly the variables in the PHTML file are represented within the CC.PHP?
ie, how do i capture the information entered in the form??

I see codes like

Array {

[’CreditCardNumber’]=> $payment->getCCNumber
}

How and where is the getCCNumber defined, I never find out.

should be a very simple question for some ppl here.. i’m really desperate.

 
Magento Community Magento Community
Magento Community
Magento Community
 
somebody
Jr. Member
 
Total Posts:  18
Joined:  2008-05-14
Leipzig, Germany
 

hi,
im actually just a noob in programming for magento, but mybe this will help you.

when you declare a block in your layout xml you will usually setup a type and a tamplate like ie this:

<block type="catalog/navigation" name="catalog.topnav" template="catalog/navigation/top.phtml"/>

the type selects the blocktype, for my understanding is this the view of the magento mvc for this example it is
stored in app/code/core/mage/catalog/block/navigation.php. his view will provide all the function which your
template-file will call with:

$this->myBlockFunction();

when you will work with an specific model or call data from a specific model you have to call the model in your block-file with:

Mage::getModel('mymodule/mymodel')

so you get an instance of this model and can work with.

hugs
sebastian

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Also in addition to that, all of the form variables are sent via _POST.  There is a getPost type function that will get the variables out and return either the entire array or just get the variables you need out of there.  I am still working it all out exactly myself but that is the gist of it.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
stereotomy
Jr. Member
 
Total Posts:  28
Joined:  2008-02-10
 

somebody thank you for the reply. I agree.

i guess this is how it works:

1) you have the getCcNumber in the phtml template in the form “cc_number”
2) in CC.php, first you define the template like you said
3) then you define your function like getCcNumber() {
$this->getData[’cc_number’]
4) you use the variable $this->getCcNumber().

as i said this is just a guess, I couldnt get this working though i feel i am close.

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Not quite, though I can only claify.  the getCcNumber() is usually linked to the database not the form variables.  That means that getCcNumber will look to cc_number in the database table and in fact you would actually use that like $this->getOrder()->getPayment()->getCcNumber(); Something like that, you could define a function like that but it would make more sense to just get the data directly out of the array.  I am having a tough time with that too and once I figure it out I will post.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Ok I was experimenting with the Poll module, easiest to work with.  Let’s look at how this is all structured.

1) /poll/active.phtml (template file)
2) /poll/controllers/VoteController.php (controller file)

To understand the form functions, it is easiest to look at this.  The active.phtml file defines the form:

<form id="pollForm" action="<?php echo $action ?>" method="post" onsubmit="return validatePollAnswerIsSelected();">
        <
div class="content">
            <
h5><?php echo $poll->getPollTitle(); ?></h5>
            
<?php if( $poll_answers ): ?>
                    
<table cellspacing="2">
                        <
tbody>
                            
<?php foreach( $poll_answers as $answer ): ?>
                            
<tr>
                                <
td><input type="radio" name="vote" class="poll_vote" id="vote_<?php echo $answer->getId() ?>" value="<?php echo $answer->getAnswerId() ?>" /></td>
                                <
td><label for="vote_<?php echo $answer->getId() ?>"><?php echo $answer->getAnswerTitle() ?></label>&nbsp;</td>
                            </
tr>
                            
<?php endforeach; ?>
                        
</tbody>
                    </
table>
            
<?php endif; ?>
        
</div>
        <
div class="actions"><button class="form-button-alt right" type="submit"><span><?php echo $this->__('Vote'?></span></button></div>
    </
form>
</
div>
<?php endif; ?>

Note the action declaration:

action="<?php echo $action ?>"

This will actually render to:

http://127.0.0.1t/magento/poll/vote/add/poll_id/1/

Note: I am on localhost the actual server name will vary for you.

This is important because what happens here is that Magento will translate this url to mean, go to the Poll directory and controllers and find the one starting with Vote.  Then look in Vote for the AddAction (similarly if you made an action TestAction the url would reflect http://127.0.0.1t/magento/poll/vote/test/poll_id/1/.

/poll/controllers/VoteController.php

Look at the Add Action
:
    public function 
addAction()
    
{
        $pollId     
intval$this->getRequest()->getParam('poll_id') );
        
$answerId   intval$this->getRequest()->getParam('vote') );

        if( 
$pollId && $answerId && !Mage::getSingleton('poll/poll')->isVoted($pollId) ) {
            Mage
::getSingleton('poll/poll_vote')
                ->
setPollId($pollId)
                ->
setIpAddress(ip2long($this->getRequest()->getServer('REMOTE_ADDR')))
                ->
setCustomerId(Mage::getSingleton('customer/session')->getCustomerId())
                ->
setVoteTime(now())
                ->
setPollAnswerId($answerId)
                ->
addVote();

            
Mage::getSingleton('core/session')->setJustVotedPoll($pollId);
            
Mage::getSingleton('poll/poll')->setVoted($pollId);
        
}

        $this
->_redirectReferer();
    
}

Alright, let’s look at this function now.  You see that $pollId and $answerId is doing a getRequest()->getParam().  If you replace the end line to var_dump($_POST) you can see what is being passed in via the POST array from the form.

var_dump($_POST);
//$this->_redirectReferer();

This will give back the POST values (if a value is picked):

array(1{ ["vote"]=>  string(1"1" }

Note that there are two getParam requests, so where is the other request?  Well if you look at the url passed as the action it contains /poll_id/1/, the first getParam will look at the url and see if there is a /{name}/{value}/ and the getParam works like getParam({name}) and then it will take whatever the value after that is.  If it doesn’t find that value it will look in the POST array for the value.  That is where the second getParam gets it value from the actual post array.  It gets the vote value which is defined in the form as name="vote".  I haven’t quite figured out how the $action value is computed but that should get you started.  It also looks like you will want to pull all the data out of the controller.

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top