write PHP inside HTML inside PHP - php

in trying to put in php code inside html that inside php,
my goal is to full this select option with an array
<?php
$wcr=array(
'Angola',
'Antigua & Barbuda',
'Armenia',
'Austria',
'Azerbaijan',
);
$content = '<div class="row">
<form role="form">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<select name="country"><option selected value="">'
<? $p=1;asort($wcr);reset($wcr);
while (list ($p, $val) = each ($wcr)) {
echo '<option value="'.$p.'">'.$val;
} ?>
'</select>
</div>
<input type="button" class="btn btn-primary" onClick="AddDoctor()"
value="Submit"></input>
</form>
</div>';
?>
i tried a different approach but nothing seems to work

Your PHP isn't inside the HTML. You ended the quoted string, so you're still in PHP execution mode, you don't need <?php again.
The loop shouldn't use echo, it should append to the $content string.
Also, each() has been deprecated, you should use a foreach loop.
<?php
$wcr=array(
'Angola',
'Antigua & Barbuda',
'Armenia',
'Austria',
'Azerbaijan',
);
$content = '<div class="row">
<form role="form">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<select name="country"><option selected value="">';
asort($wcr);
foreach ($wcr as $p => $val) {
$content .= '<option value="'.$p.'">'.$val.'</option>';
}
$content .= '</select>
</div>
<input type="button" class="btn btn-primary" onClick="AddDoctor()"
value="Submit"></input>
</form>
</div>';
?>

Related

PHP form only returning empty array

I am working on a code segment that is a messaging function next to a list of names. On the page there is an envelope and when you click it a pop-up window appears with a text area inside of it. I would like to be able to fill that textarea with characters and send it as a message using my already functioning sendMessage function. I know my sendMessage function works and I've isolated the issue to the return given by the submit button. How do I get my submit button to POST the textarea to the page (keep in mind there are 2+ forms on this page). The code looks like this:
if (empty($_POST) === false) {
print_r($_POST);
$blah = $_POST;
echo "<script>window.top.location='asd.$blah'</script>";
}
if (empty($_POST['Send']) === false) {
echo "<script>window.top.location='../hidden/PNMasdd.php?gpa=$var1&year=$var2&major=$var3&sport=$var4'</script>";
echo 'YOU ARE IN THE SENDING MESSAGE FIELD';
if( empty ($_POST['message_full']))
{
$errors[] = 'All fields must be filled in!';
//print_r($errors);
}
if(empty($_POST) === false && empty($errors) === true)
{
$receiver_id = $temp_user['id'];
$sender_id = $user_data['id'];
$type = 0;
if(empty($is_Convo))
{
$is_Convo = 0;
}
$blahVar = $user_data['id'] . ',';
$message = str_replace("\r\n", "<br>", $_POST['message_full']);
$message_data = array(
'sender_id' => $user_data['id'],
'receiver_id' => $receiver_id,
'message_full' => $message,
'is_opened' => $blahVar,
'isConvo' => $is_Convo,
'type' => $type
);
//Put data limit code here
send_message($message_data);
$my_exten = "Messages/messages.php";
$noti_data = array(
'id_receive' => $receiver_id,
'id_sent' => $user_data['id'],
'type' => 1,
'exten' => $my_exten
);
notify($noti_data);
echo '<center>';
echo '<div class="alert alert-info">
<strong>It sent!</strong> You have sent a message successfully!
</div>
';
echo '</center>';
}
else{
echo output_errors($errors);
}
}
?>
<form id="wow" name="wow" action="" method="POST">
<div class="row">
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<center>
<textarea maxlength="100000" data-msg-required="Please enter your message." rows="10" cols="100%" class="form-control" name="message_full" id="message_full"></textarea>
</center>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<table width=90%><td width=45%><button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button></td><td width=45%>
<button type="submit" value="Send Message" name="Send" onclick="document.getElementById('wow').submit()" class="btn btn-primary">Send</button>
As pointed out in the comments, the buttons need to be inside your form.
Your setup is a bit odd for my taste, but try this out.. it will mostlikely need some tweaks and i'm not sure about the cancel button..
<form id="wow" name="wow" action="" method="post" >
<div class="form-group required">
<div class="col-md-12">
<textarea name="message_full" id="message_full" rows="10" cols="100%" class="form-control" maxlength="10000" placeholder="Please enter your message." autocomplete="off"></textarea>
</div>
</div>
<div class="buttons"> <!-- add css for this or change the class to yours.. -->
<input class="btn btn-default" data-dismiss="modal" value="Cancel" />
<input class="btn btn-primary" type="submit" value="Send Message" />
</div>
</form>
As #Barmar just said, you don't have any field named 'Send'. You should change:
if (empty($_POST['Send']) === false)
to:
if (empty($_POST['message_full']) === false)
(Edit)
Only the fields inside your form will get to $_POST superglobal, the submit button will not get there

dropdown select value in yii

I am currently trying to create a sample shopping site,The problem came when I created a search page.In my search page I Have two fields one for enter the keyword and other for select type of item
Which means search,keyword in the selected item.but the item defined in DB is 0 and 1 and i Trying to get it my code but fails...could any one help me...
The Gfunction code is
public static function item(){
$optionList='';
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
foreach($CategoryList as $catdata)
{ $optionList.="<option value='".$catdata['type']."'>".$catdata['name']."</option>"; }
return $optionList;
}
and view code is
<form action="<?php echo Yii::app()->baseUrl; ?>/offer?>" method="GET" class="form-inline form-section-2 row fadeInDown animated">
<div class="col-sm-5 form-group">
<input type="text" name="search" class="form-control" id="search" value="" placeholder="Enter Your Keyword">
</div>
<div class="col-sm-4 form-group">
<select name="cat" class="form-control selectpicker">
<option value='0'>Select type</option>
<?php echo GFunctions::item(); ?>
</select>
</div>
<div class="col-sm-3 form-group">
<button type="submit" class="btn btn-default btn-new">Search</button>
</div>
</form>
why don't you use Yii built in functions to handle the dropdown?
I am not sure how your Models looks like but you can choose one of the following functions to achieve what you want instead of trying to return a String $optionList with HTML tags.
CHtml::activeDropDownList() // for directly bind to active record model
CHtml::dropDownList()
Try this code....
Function
public static function item(){
$CategoryList = OfferEvents::model()->findAllByAttributes(array());
$optionList = CHtml::listData($countries, 'type', 'name')
return $optionList;
}
In view
<?php echo CHtml::dropDownList('cat', '', GFunctions::item(), array('prompt'=>'Select type'));?>

PHP Search not passing query on URL

I have a search function on my PHP (CodeIgniter) website. But I want to pass the query terms in the URL so Google Analytics can track that info. I think this is kind of basic but I'm just learning how to code.
This is my HTML:
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="post" action="<?php echo base_url('cursos/searchCourse'); ?>">
<div class="form-group">
<input class="form-control" type="text" name="search_course" placeholder="Busque um curso" />
<input type="hidden" name="url" value="<?php echo current_url(); ?>" />
</div>
<button class="btn btn-md btn-default" type="submit" value="">
<span class="glyphicon glyphicon-search"></span>
</button>
<?php if($this->session->flashdata('search_error')) : ?>
<?php $errors = $this->session->flashdata('search_error');echo $errors['search_course']; ?>
<?php endif; ?>
</form>
This is the function searchCourse:
public function searchCourse(){
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="alert alert-danger mtop5" role="alert">', '</p>');
$this->form_validation->set_rules('search_course', 'Busca', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_message('required', 'Campo de preenchimento obrigatório');
$this->form_validation->set_message('min_length', 'O campo precisa conter pelo menos 2 caracteres');
if ($this->form_validation->run()){
$search = $this->curso_model->getCoursesBySearch($this->input->post('search_course'));
if(!empty($search)){
$this->index($search);
}else{
$this->set_flashdata('search_empty', 'search_empty', $this->input->get('url'));
}
}else{
$errors = array('search_course'=>form_error('search_course'));
$this->set_flashdata('search_error', $errors, $this->input->get('url'));
}
}
And this is my model:
public function getCoursesBySearch($search){
$this->db->select('*');
$this->db->from('curso');
$this->db->group_by('curso_nome');
$this->db->like('curso_nome', $search);
//$this->db->or_like('curso.curso_descricao', $search);
//$this->db->or_like('curso.curso_categoria', $search);
$this->db->or_like('tags.tags_nome', $search);
$this->db->join('curso_tags', 'curso_tags.curso_id = curso.curso_id');
$this->db->join('tags', 'tags.tags_id = curso_tags.tags_id');
//$this->db->where('curso_status', 1);
//$this->db->distinct();
$this->db->distinct();
$query = $this->db->get();
print_r($search);
//exit;
return $query->result();
}
Can anybody give me a clue?
Cheers!
Use jquery to modify the action attribute dynamically. Assign id attritube to each of the form elements including form itself
HTML :
<form id="my_form" method="post" action="dummy.html">
<input id="search_text" type="text" name="search_course" placeholder="Busque um curso" />
<input type="hidden" name="url" value="sdff" />
<button id="my_button" type="submit" value="Submit"> </button>
</form>
Jquery: (Add below your form)
<script>
$('#my_button').click(function(){
var search_text = $("#search_text").val();
// Replace my_search.php with actual landing page
$('#my_form').attr('action', 'my_search.php' + '?search=' + search_text);
});
</script>
Note : Your search text will be decoded in URL (in case of spaces, etc) and you have decode the URL at the landing page.
Change the method from post to get, so the values will be in the URL :
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="post" action="<?php echo base_url('cursos/searchCourse'); ?>">
replace with :
<form class="navbar-form navbar-right mtop10 mright10 hidden-xs" role="search" method="get" action="<?php echo base_url('cursos/searchCourse'); ?>">

How to add an PHP IF statement in this Script?

$sql = "SELECT * FROM item";
$stmt = $conn->prepare($sql);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
<div class="content-boxes style-two top-column clearfix animated flipInY" style="opacity: 1;">
<div class="content-boxes-text">
<form action="php/additem.php" method="post" class="form-inline pull-right">
<h4>'.$row['itemName'].'</h4><input type="hidden" name="itemName" value="'.$row['itemName'].'">
<img src="../wholesale/img/sourdough.jpg" class="img-reponsive">
<p>'.$row['description'].'</p><input type="hidden" name="description" value="'.$row['description'].'">
<div class="form-group">
<label class="sr-only" for="exampleInputAmount">Qty</label>
<div class="input-group">
<input type="number" name="qty" class="form-control" id="exampleInputAmount" placeholder="How Many?">
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
<!-- //.content-boxes -->
</div>
';
}
I want to be able to add an if statement in this result_2 string.
This is for displaying a product, and i would like to display price depending on users session value?
eg.
if ($_SESSION['customer_x'] == a) {
display price a
}
else if ($_SESSION['customer_x'] == b) {
display price b
}
Is this the correct method to be able to add an if statement to a JSON query?
After Starting your while loop, put a if else there,
$price ="";
if ($__SESSION['customer_x'] == a) {
$price='display price a';
}
else if ($_SESSION['customer_x'] == b) {
$price='display price b';
}
and now echo this price where ever you want to in your html
this is more neet and less messy way
You can use a ternary if operator to have conditional statements inside strings, example
$bool = false;
echo "The value of \$bool is " . ($bool == true ? "TRUE" : "FALSE");
Example in use
To add that you can keep doing the same but:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){ // the next of the content
$priceOfSession = ($__SESSION['customer_x'] == a) ? 'pricea' : 'priceb';
$data['result_2'] .= '
<div class="col-sm-4 col-md-4">
'.$priceOfSession.'
</div>
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
<!-- //.content-boxes-text -->
</div>
</div>
';
}
So if you want to evaluate only two conditions if otherwise the if you want, simply add it there. Do as you add the $ row but before defining value.

Yii 2 - radioList Template

I want to add template to radioList in yii2, which I tried, but I am unable to get the proper o/p.
The HTML is
<div class="input-wrap">
<label class="gender-head">Gender</label>
<label class="signup-radio">
<input type="radio" name="signup-gender" id="signupMale" checked tabindex="3" />
<i></i>
<span>Male</span>
</label>
<label class="signup-radio">
<input type="radio" name="signup-gender" id="signupFemale" tabindex="3" />
<i></i>
<span>Female</span>
</label>
</div>
The o/p should look like this
The Yii2 code which I tried is...
<div class="input-wrap">
<div class="clearfix">
<?= $form->field($model, 'gender', ['radioTemplate' => '<label class="gender-head">{label}</label><label class="signup-radio">{input}</label>'])->inline()->radioList([1 => 'Male', 0 => 'Female'], ['separator' => '', 'tabindex' => 3]); ?>
</div>
<div class="help-block"></div>
</div>
I have searched a lot on the template but did not get any proper response.
I finally got the way through which we can modify the input tag generation logic in Yii2
To get the above result of the radio buttons, I have developed the following code
<div class="input-wrap">
<div class="clearfix" id="UserLogin-gender">
<label class="radio-head">Gender</label>
<?=
$form->field($model, 'gender')
->radioList(
[1 => 'Male', 0 => 'Female'],
[
'item' => function($index, $label, $name, $checked, $value) {
$return = '<label class="modal-radio">';
$return .= '<input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3">';
$return .= '<i></i>';
$return .= '<span>' . ucwords($label) . '</span>';
$return .= '</label>';
return $return;
}
]
)
->label(false);
?>
</div>
<div class="help-block"></div>
</div>
The "item" option in the radioList is a callback function to the input generation logic written in Yii2. We can easily modify the layout of each element generated using this callback function and it's parameters.
maybe i'am too late but you can try this
<?= $form->field($model, 'abc')->inline()->radioList(['example1' => 'example1', 'example2' => 'example2'])->label(false) ?>
Source
$form->field($model, 'gender')
->radioList(array(1 => 'Male', 0 =>'Female'), array('class' => 'i-checks'));

Categories