PHP: Confirmation Dialog without using JavaScript? - php

I have a very simple form:
<form action="system/web/push.php" method="post">
<div class="row">
<div class="col card_input_push card_input_push_language">
<select name="<?php echo PARAM_PUSH_LANGUAGE; ?>">
<?php echo get_languages($mysqli); ?>
</select>
</div>
</div>
<div class="row">
<div class="col card_input card_input_push">
<input type="text" name="<?php echo PARAM_PUSH_TITLE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_TITLE]; ?>"<?php echo set_value(PARAM_PUSH_TITLE); ?>required/>
</div>
</div>
<div class="row">
<div class="col card_input_push card_input_push_message">
<textarea name="<?php echo PARAM_PUSH_MESSAGE; ?>" placeholder="<?php echo $language_array[LV_PUSH_INPUT_MESSAGE]; ?>"<?php echo set_value(PARAM_PUSH_MESSAGE); ?>required></textarea>
</div>
</div>
<div class="row">
<div class="col text-center card_input_push card_button_push_send">
<button class="btn btn-default" type="submit" name="<?php echo REQUEST_SEND_GCM; ?>" value="<?php echo $language_array[LV_PUSH_INPUT_SEND]; ?>"><?php echo $language_array[LV_PUSH_INPUT_SEND]; ?></button>
</div>
</div>
</form>
When i now press the submit button i dont want the post to perform instantly. I first want a confirmation dialog to show up with yes or no answers and only if the user says yes i want the post action to be performed.
What i NOT want is to use JavaScript. Is there a way to do that?
EDIT
Thank's for the answers. To satisfy all respondents i would like to tell you, why i dont want to use JavaScript. The answer is simple. I have no idea about JavaScript. I never have used it before but the project im working on (as always) has to be finished ASAP. That's the only reason.

Yes there is a way, use GET (not POST) the first time, on the PHP file check if GET was used and if so generate a form that sends confirmation form back to the user. Then make confirmation form to submit using POST and you are done.
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
// generate confirmation form
}elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
// process your data
}
If anyone has access to a book called Head First PHP-MySQL they cover a similar example on page 280.

Maybe you could do something like this, with some css for design your dialog.
change your form with :
<form action="this_page">
If you submit, you call this page, display the dialog with a form, and when you submit with second form, you post to your needed php file.
if (isset($_POST['your_submit'] )) {
echo "<yourdialog>";
echo "<form action="system/web/push.php">";
echo "content";
echo "<submit yes>";
echo "</form>";
echo "</yourdialog>";
}

Your workflow is:
gather form data
send to server
get confirmation that that's what the user wants.
if yes then process data.
If you want to do that all via php then the server's handling the full load, so you'll want two forms, one to collect and one to confirm. And you'll want to store the first form's data so you can process it according to the second form's result. A bit old fashioned but not difficult.

Related

Codeigniter : Pass data from view to controller using anchor tag

I am working on a project where I need to pass an ID to controller when someone clicks on the link, and open response in new tab.
I googled for solution on this, but couldn't find proper result. I also tried by sending data through URL in anchor tag, but then it gave an error of config.php file, so I reversed the code.
Currently I am using form tag to pass data. Can ye do this using PHP, or will we have to take help of jquery?
Here's View code:
<form class="form-horizontal" method="post" action='<?php echo base_url() . "home/edit_policy"; ?>'>
<div class="form-group">
<input type="hidden" class="form-control" id="polid" name="polid" value="<?php echo $ajax_view_pol_response[0]->polid; ?>">
<div class="col-sm-offset-3 col-sm-3">
<button class="btn btn-primary" id="editpolsubmit">Edit this Policy</button>
</div>
</div>
</font>
This code is from Controller:
public function edit_policy() {
$polid = $this->input->post('polid');
}
I want to transfer the "polid" from view to controller function using anchor tag.
All positive suggestions are appreciated.

How can I upload information from a php form on a different site?

I'm creating a form that needs to upload the information retrieved to an entirely separate site.
My form's "action" is to send the information to itself in order to validate information as well as see, if the required fields are input. If no errors are found, an email is sent with a message that includes the information from the form.
My code for the index page generally looks like this:
<?php include('action_page.php'); include('config.php')?>
<form method="POST" name="email_form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="row">
<div class="col-25">
<label for="subject"><span class="error">* <?php echo $titleErr;?></span> Subject <i class="glyphicon glyphicon-pencil"></i></label>
</div>
<div class="col-75">
<textarea id="subject" name="subject" placeholder="Please input a subject for the email" style="height:46px"><?php echo $title;?></textarea>
</div>
</div>
<hr>
<div class="row">
<div class="col-25">
<label for="emails"><span class="error">* <?php echo $emailErr;?>
</span> Email <i class="glyphicon glyphicon-envelope"></i></label>
</div>
<div class="col-75">
<p style="color:#0071c5">Please choose which group(s) to send this to:
</p>
<?php foreach($emails as $value): ?>
<input type="checkbox" name="email[]" value='<?php echo $value ?>'<?php if (isset($_POST['email']) && in_array($value, $_POST['email'])) echo 'checked="checked"';?> ><?php echo $value ?>&nbsp &nbsp &nbsp
<?php endforeach; ?>
</div>
</div>
My action page then sends an email using the php mail() function.
What I want to do is upload the body of the email on a separate website, which runs similarly to how Wikipedia is run - anyone can edit a page even if you're not the owner of the page.
To be honest, I'm not entirely sure how to even start. I'm able to use a header on my action page that can link my form to a different page, but this doesn't autofill like I want it to.
For example:
if(mail($mail_to, $title, $msg, implode("\r\n", $headers))){
header('Location: https://en.wikipedia.org/w/index.php?title=Japanese_aircraft_carrier_Ry%C5%ABj%C5%8D&action=edit');
}
sends the user to the appropriate Wikipedia page, but I'm not able to see any of my variables, that my form stored.
How do I solve the problem?
It's hard to say without seeing more of your code, but you will likely need API access to perform a POST request. Here is documentation from Wikipedia about working with their API in PHP.

Submitting a form without action or method or any other javascript code

A friend asked me add pattern code to prevent typing email ids and phone numbers, on his WP template site.
As i have added the pattern code to the input field, it still accepts email ids and numbers.
So, i figured that may be the form submit php code got some changes needed. but trickily his form tag doesn't have any action tag,. it just have form ID.
I don't know what to do. I just need to make the pattern work in the input field to prevent email id's and numbers.
the code goes as:
<div class="col-xs-12 col-sm-8 col-md-9">
<div class="message-entry">
<form id="message_reply_form">
<div class="form-group" >
<input type="text" autocomplete="off" class="form-control mb-10" placeholder="<?php esc_html_e( 'Your Message', 'extretion' ); ?>" name="reply_message" pattern="^(?!.*([a-zA-Z0-9._%+-]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)).(?:\b\d{10,11}\b)[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]" >
</div>
<input type="submit" class="btn btn-primary btn-sm reply_message_btn" value="<?php esc_html_e( 'Reply', 'extretion' ); ?>">
</form>
</div>
</div>
Any help is appreciated..
network screenshot at the time of message submit
In HTML5, the action attribute is no longer required.
source
if I remember correctly, it defaults to current page. This is why the form is still submitted.
Have you tried giving a action attribute to your form?
<form action='your/url'>
The body of your form
</form>

Understanding Joomla default login

I'm fairly new to Joomla and Php in general. I need to understand how does Joomla perform login operation (via php code). The following is the php snippet that performs the operation:
<form action="<?php echo JRoute::_('index.php?option=com_users&task=user.login'); ?>" method="post">
<fieldset>
<?php foreach ($this->form->getFieldset('credentials') as $field): ?>
<?php if (!$field->hidden): ?>
<div class="login-fields"><?php echo $field->label; ?>
<?php echo $field->input; ?></div>
<?php endif; ?>
<?php endforeach; ?>
<?php if (JPluginHelper::isEnabled('system', 'remember')) : ?>
<div class="login-fields">
<label id="remember-lbl" for="remember"><?php echo JText::_('JGLOBAL_REMEMBER_ME') ?></label>
<input id="remember" type="checkbox" name="remember" class="inputbox" value="yes" alt="<?php echo JText::_('JGLOBAL_REMEMBER_ME') ?>" />
</div>
<?php endif; ?>
<button type="submit" class="button"><?php echo JText::_('JLOGIN'); ?></button>
<input type="hidden" name="return" value="<?php echo base64_encode($this->params->get('login_redirect_url', $this->form->getValue('return'))); ?>" />
<?php echo JHtml::_('form.token'); ?>
</fieldset>
</form>
To my understanding, the first line perform POST request to post username and password to the server. In addition, a session token, should be retrieved upon successful validation. However, I could not figure out where exactly are the username and the password (that are inserted by the user and supposed to be sent via POST request) in this line. Or, where are they retrieved from? In addition, where is the response content (session Token) is stored?
My objective is to mimic the request using JavaScript so that I can perform login using external mobile App. In other words, I need to write the same logic in JavaScript so I can login to the website from mobile App.
The fields are retrieved from login.xml file from the following path \components\com_users\models\forms. getFieldset('credentials') will get field details of the fieldset in the name "credentials" <fieldset name="credentials" in the above mentioned file.And you can use foreach to display the field. You can refer this link for more info.

How to pass php variable to modal window

I am making a simple page and I have found this little problem. I have this in my template:
<?php foreach ($this->vypis_serie as $value) : ?>
<div class="serie">
<div id="serie_header">
<?= $value['nazev_cviceni'] ?>
</div>
<div id="serie_info">
<p>Number of excercises: TODO</p>
<p>Sport type: <?= $value['typ'] ?></p>
<p>KCal summary: <?= $value['kcal'] ?></p>
</div>
<div class="button_upravit">Edit</div>
<div class="button_smazat">Delete</div>
</div>
<?php endforeach; ?>
basically it is a block that fills in information about particular exercise (it is a sport app). SO If I have 3 entries in DB, it will print this code three times with corresponding info.
The problem I have is with the edit button, which upon clicking opens modal window. It is made purely with CSS, so no Javascript.
When I click the button, it jumps to this code:
<div id="openModal_edit" class="modalDialog">
<div>
X
<div id="editace">
<form id="platba" action="serie/edit/" method="post" enctype="multipart/form-data">
<fieldset>
<legend>Edit serie</legend>
<ol>
<li>
<label for="name">Name of the series</label>
<input id="name" name="nazev_cviceni" type="text" required autofocus>
</li>
<li>
<label for="typ">Sport type</label>
<select name="typ">
<option value="Kolo">Bike</option>
<option value="Běhání" selected="selected">Running</option>
</select>
</li>
</ol>
</fieldset>
<fieldset>
<button type="submit">Save</button>
</fieldset>
</form>
</div>
</div>
</div>
But since I jump to div id and I am not using a new page where I could choose a controller and pass a variable, I need somehow to pass the variable (id of the exercise) to the modal window, so I can know which of the possible buttons I have clicked. Is there any way to do it, without the need to rewrite all other pages where I have used this modal window?
I can't use another foreach like in the first part, because the modal window is always a single object that appears, unlike all the entries on the page that are there as many times as there are entries in the DB.
Hope that it is understandable, sorry for my English :)
The simplest way to do this using a single modal window involves adding some javascript code to your page.
First, add the relevant information to the edit link, with a new data-serie-<name> for each piece of data you want to pass:
<a href="#openModal_edit" data-serie-name="<?= $value['nazev_cviceni'] ?>" ...>
Next, add an onclick event handler to that same link. This handler will extract the embedded data from the <a> element and inject it in the modal window. The dataset element provides access to the data-* attributes from javascript
// Example
onclick="serieName=this.dataset.serieName;document.querySelector('#openModal_edit input#name').value = serieName;return true;"

Categories