0

I'm trying to sharpen my PHP skills, but am at a loss when it comes to arrays.

Here's the deal: I'm trying to set the value of a form element to be $emx - a variable that catches a parameter from a URL string i.e. [email protected]

Here is the array I have, which builds the form (this is PHP Yii)

'email' => array('type'=>'text', 'label'=>$this->t('Your email address'), 'value' => $emx, 'onFocus' => 'this.value=""'),

The HTML that it produces:

<input type="text" id="MDealSubscribeForm_email" name="MDealSubscribeForm[email]" onfocus="this.value=&quot;&quot;" value="">

I have defined $emx as the following:

<?php
    if ($_GET['emx'] != ""){
    $emx = $_GET['emx'];}
    else {
    $emx = "Enter your email address";} ?>

The problem I'm having is that the value is not being set to $emx - regardless of it I append ?emx= to the URL or not, value is always ""

UPDATE: The php for if ($_Get['emx']... is in one file (a wrapper) and the 'email' => array(... is in a separate file - if that matters.

Do I have the array syntax wrong? I thought I'd simply need to put in $emx to set the value. Thanks for reading!

8
  • 1
    You forgot to mention the question / problem you are having. Commented Oct 9, 2012 at 20:56
  • 1
    Syntax looks OK... I find it best to use isset($_GET['emx']). Right before you make that array, var_dump($emx); to see what it is. Commented Oct 9, 2012 at 20:57
  • @PeeHaa i assume value should be populated with what was in $emx Commented Oct 9, 2012 at 20:57
  • print_r($array); <<or what ever the array is called Commented Oct 9, 2012 at 20:58
  • @PeeHaa Trying to populate it with whatever $emx is (either the email or the string Enter your email address) Commented Oct 9, 2012 at 20:59

4 Answers 4

1

MDealSubscribeForm is the model you are using to build your form (I am calling it as $model in my answer )

if(isset($_GET['emx'])){
    $model->email = $_GET['emx'];
} else {
    $model->email = "Enter your email address";
}

remove 'value' => $emx , this is not needed anymore.

--

and also, try 'onFocus' => 'js:this.value=""' and I believe this will give correct html instead of &quot;&quot;

Sign up to request clarification or add additional context in comments.

Comments

0

You should use $_GET['MDealSubscribeForm']['email'] (if it's a GET)

Next time just do a print_r($_GET); to debug forms

1 Comment

I'm not sure what you mean, but I'm grabbing the email from the URL fine. I'm just unable to get it to show for value= in the form, which is built by the array.
0

It might be better to use a placeholder attribute on the email input field, rather than explicitly setting a value if the email is not set. I always hate seeing inline javascript.

Try:

<?php $emx = (isset($_GET['emx'])) ? $_GET['emx'] : ''; ?>

and then:

'email' => array('type'=>'text', 'label'=>$this->t('Your email address'), 'value' => $emx, 'placeholder' => 'Enter your email address'),

Back on topic, this does not look like an array designed for the standard Yii form input generator function. Maybe the problem is in the code that converts this array to the input HTML? Can you tell us or show us the code that does this?

Comments

0

You're getting a lot of wrong answers because you thought this was a PHP question and titled your question as so. From everything you've said, it looks likes $emx is set properly, the trouble is that you can't get it to show in the Yii generated form.

Note that there is no 'value' property for a CFormInputElement. To set the value, you need to set it in your model, e.g.:

$model->email = $emx;

You could also avoid the else statement, by setting the default value in your model or the form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.