1

Im attempting to allows users to choose from a simple set of 4 avatars that they can use as their profile picture.

I've tried using:

<input type="image" class="profile-image-icon" src="img/avatar/avatar1.png" name="image" id="image">

But I finally found that using input type="image" is only for using an image as a submit button.

The avatars in question are already in my img/avatar folder, which I then only want to store the image path in the database.

I have echoed any error messages on changeProfile.php, which is returning "Nothing getting through" so I know it is an issue with my form, but I am unsure how to proceed.

Is there anyway to allow the user to simply click on the image and it 'POSTS' to changeProfile.php where I can then retrieve the file path to store in my database?

Thanks for any help provided!

edit_profile_image.php

<form class="profile-image-form" method="POST" action="profileChanged.php">                       

<fieldset>
<div class="image-control">
<input type="image" class="profile-image-icon"src="img/avatar/avatar1.png" name="image" id="image">
</div>
<?php echo "<input type='text' class='form-control' id='studentNumber' name='studentNumber'  value='$studentID'>"; ?>  
</fieldset>                      

</form> 

changeProfile.php


if (isset($_POST['image'])) {

$image = mysqli_real_escape_string($conn, $_POST['image']);
$studentNumber = mysqli_real_escape_string($conn, $_POST['studentNumber']);

} else {
    echo "Nothing coming thorugh";
}
  • 1
    Are you averse to using javascript and doing this via ajax? Alternatively, you could try using HTML radio input and just having the values of the inputs correspond to the image? – Dylan Apr 09 '19 at 17:06
  • Possible duplicate of [Use images like checkboxes](https://stackoverflow.com/questions/30663562/use-images-like-checkboxes) – Funk Doc Apr 09 '19 at 17:06
  • `input type="image"` only makes a submit button out of an imagem... source: https://www.w3schools.com/tags/att_input_type_image.asp and that's why you won't get anything from `$_POST['image']` – MonneratRJ Apr 09 '19 at 17:08
  • I suggest you take @Dylan advice and go with the javascript route... make a hidden field that gets the path of the imagem through javascript and sets it in that field and grab that field later on... – MonneratRJ Apr 09 '19 at 17:09
  • **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use manual escaping and string interpolation or concatenation to accomplish this because you will create severe [SQL injection bugs](http://bobby-tables.com/). Accidentally unescaped data is a serious risk. Using bound parameters is less verbose and easier to review to check you’re doing it properly. – tadman Apr 09 '19 at 17:10
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and ideally should not be used in new code. – tadman Apr 09 '19 at 17:10
  • (And [PDO](http://php.net/pdo) is even less verbose when you're dealing with prepared statements.) – miken32 Apr 09 '19 at 18:12

0 Answers0